View source: R/mod_modifyDatasets.r
calculateVariable | R Documentation |
Calculate values for a new variable.
calculateVariable(dataset, cexpr = expression(""), name = NULL, type = "c")
dataset |
The standard dataset as produced by |
cexpr |
An arbitrary R expression describing how to calculate the values for the new variable. The expression will be evaluated using 'with', with the local environment being a single row of the provided dataset. |
name |
Character length one. The name of the new variable. |
type |
Character length one. The type of the new variable. Possible values are 'c' for class-variables and 'n' for numeric variables. Defaults to '"c"'. |
Similarly as combineVariable
is simply combining , i.e.
pasting together all the values of the selected variables from a single
observation (within one row), calculateVariable
is using variables of
one observation (within one row) to calculate a new value according to the
expression provided in the argument 'cexpr'. The expression can contain logical
clauses etc, and it pertains exclusively to the values within a single row.
Typically, the expression could contain an if clause followed by two values,
See examples.
New variables of type 'numeric' can obviously only be calculated from numerical
variables, but the numeric result of such a calculation can be used as class
variable by setting the type of the new variable to 'class' via providing 'c'
to the argument 'type'.
The name of the new variable and the expression used to calculate it is stored
in a list in the slot named 'calcVarInfo'. (object@calcVarInfo
)
An object of class 'aquap_data' with a new variable in the header.
combineVariable
Other dataset modification functions:
[,aquap_data-method
,
combineVariable()
,
generateMergeLabels,aquap_data,aquap_data,character,character-method
,
mergeDatasets,aquap_data,aquap_data,missing-method
## Not run:
fd <- gfd() # load a dataset
cexpr <- expression(Y_Foo + Y_Bar)
newFd <- calculateVariable(fd, cexpr, name="Addition")
# more examples of how to formulate the expression:
cexpr <- expression((Y_Foo * Y_Bar)/Y_Fuba)
cexpr <- expression(if (Y_Foo < 5 & C_Bar == "blabla") "Outcome1" else "Outcome2")
cexpr <- expression(if (C_Bar == "blabla" | C_Foo == "blibli") "Outcome1" else "Outcome2")
cexpr <- expression(as.numeric(paste0(Y_Foo, C_Bar))/Y_Foobar) # of course crazy, but
# it demonstrates that everything is possible in the expression.
#
# use a numerical outcome as class variable:
newFd <- calculateVariable(fd, expression(Y_Foo - Y_Bar), name="Subtraction", type="c")
newFd <- calculateVariable(fd, expression(Y_Foo - Y_Bar), name="Subtraction", type="n")
# in that way you can color by the result of the calculation, and you
# can also add the same variable as numerical variable
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.