Title: | An Implementation of a SAS-Style Data Step |
---|---|
Description: | Based on a SAS data step. This allows for row-wise dynamic building of data, iteratively importing slices of existing dataframes, conducting analyses, and exporting to a results frame. This is particularly useful for differential or time-series analyses, which are often not well suited to vector- based operations. |
Authors: | Brandon Taylor |
Maintainer: | Brandon Taylor <[email protected]> |
License: | CC0 |
Version: | 0.0.2 |
Built: | 2025-03-13 03:31:01 UTC |
Source: | https://github.com/cran/datastepr |
An implementation of a SAS datastep in a class
dataStepClass
dataStepClass
An R6Class
generator object
i
i
begins at 0 and is incremented for each iteration of the data step.
results
The results
frame is initialized as an empty data frame.
It is populated row-wise with each iteration of the data step.
continue
continue
is a marker which signals that the step should
continue repeating. When continue
is 1, repetition will continue, and when
continue
is 0, repitition will cease. It is initialized to 0.
eval
eval
is initialized as NULL, but will store a pointer to
the current evaluation environment. This pointer helps pass the evaluation
environment from one iteration of the data step to the next.
begin(env)
begin
does three things: imports the environment of the previous
step to the current, stores the current environment (or the environment specified),
and increments i
by 1. It takes one argument, envir
, which should
typically be set to environment()
.
set(dataframe, group_id)
set
takes two arguments: a data frame and an optional unquoted group_id
variable. This group_id
variable must contain a consecutive sequence of natural
numbers from 1 to some maximum. In each data step, rows where i
matches the
group_id
variable (or simply the ith row if no group_id variable is given) are selected,
and the slice is split into vectors and imported into the evaluation environment.
continue
is set to 0 once set
reaches the maximum value in the group_id
column, ceasing repetition of the datastep, else continue
is set to 1.
set_(dataframe, group_id)
A standard evaluation version of set_
, in which the group_id
variable is included as a string, formula, or lazy object.
output
output
takes an optional list argument. Either the list, or, if none is given,
all vectors in the evaluation environment are gathered into a data.frame, and this data.frame
appended to results
.
end
end
will, if continue
is 1, evaluate
the function given within the evaluation environment. Typically, the function
given will be the current function: that is, steps are joined recursively.
step = dataStepClass$new() frame = data.frame(x = 1:10) stairs = function() { step$begin(environment()) step$set(frame) y = x + 1 step$output() step$end(stairs) } stairs() step$results
step = dataStepClass$new() frame = data.frame(x = 1:10) stairs = function() { step$begin(environment()) step$set(frame) y = x + 1 step$output() step$end(stairs) } stairs() step$results
Convert an object to a list, select only vector entries, coerce to a data.frame, and append to the given data frame.
toDf(object, dataframe)
toDf(object, dataframe)
object |
An object which can be coerced to a list (e.g. an environment) |
dataframe |
A data frame |
An appended dataframe
toDf(list(a = 1, b = 2, data.frame()), data.frame()) toDf(environment(), data.frame())
toDf(list(a = 1, b = 2, data.frame()), data.frame()) toDf(environment(), data.frame())
A function to coerce an object to a list and append the list to an environment
toEnv(object, environment)
toEnv(object, environment)
object |
An object which can be coerced to a list (e.g. an environment) |
environment |
An environment |
An appended environment
toEnv(data.frame(a = 1, b = 2), environment()) toEnv(list(a = 1, b = 2), environment()) toEnv(environment(), new.env())
toEnv(data.frame(a = 1, b = 2), environment()) toEnv(list(a = 1, b = 2), environment()) toEnv(environment(), new.env())