Learn R Programming

expss (version 0.5.5)

compute: Experimental functions for operations with default dataset

Description

Workflow for these functions is rather simple. You should set up default data.frame with default_dataset and then operate with it without any reference to your data.frame. There are two kinds of operations. The first kind modify default dataset, the second kind will be evaluated in the context of the default dataset but doesn't modify it. It is not recommended to use one of these functions in the scope of another of these functions. By now their performance is not so high, especially .do_if/.modify_if can be very slow.

Usage

.modify(expr)
.modify_if(cond, expr)
.do_if(cond, expr)
.compute(expr)
.with(expr, ...)
.val_lab(...)
.var_lab(...)
.set_var_lab(x, ...)
.set_val_lab(x, ...)
.add_val_lab(x, ...)
.if_val(x, ...)
.recode(x, ...)
.fre(...)
.cro(...)
.cro_cpct(...)
.cro_rpct(...)
.cro_tpct(...)
.cro_mean(...)
.cro_sum(...)
.cro_median(...)
.cro_fun(...)
.cro_fun_df(...)
.set(varnames, value = NA)

Arguments

expr
set of expressions in curly brackets which will be evaluated in the context of default dataset
cond
logical vector/expression
...
further arguments
x
vector/data.frame - variable names in the scope of default dataset
varnames
character vector. Names of variables which should be created in the default dataset. Expressions inside backticks in varnames will be expanded as with subst.
value
value/vector/matrix/data.frame. Value for newly created/existing variables.

Details

Functions which modify default dataset:
  • .modify Add and modify variables inside default data.frame. See modify.
  • .compute Shortcut for .modify. Name is inspired by SPSS COMPUTE operator. See modify.
  • .modify_if Add and modify variables inside subset of default data.frame. See modify_if.
  • .do_if Shortcut for .modify_if. Name is inspired by SPSS DO IF operator. See modify_if.
  • .where Leave subset of default data.frame which meet condition. See where, subset.
  • .set_var_lab Set variable label in the default data.frame. See set_var_lab.
  • .set_val_lab Set value labels for variable in the default data.frame. See set_val_lab.
  • .add_val_lab Add value labels for variable in the default data.frame. See add_val_lab.
  • .if_val Change, rearrange or consolidate the values of an existing variable inside default data.frame. See if_val.
  • .recode Shortcut for .if_val. Name is inspired by SPSS RECODE. See if_val.
  • .set Set variables values in the default dataset with given names filled with value. It is possible to set multiple variables at once. Expressions inside backticks in varnames will be expanded as with subst. set (without dot) is also available inside .compute, .modify, .modify_if, .do_if, modify, modify_if.

Other functions:

  • .var_lab Return variable label from default dataset. See var_lab.
  • .val_lab Return value labels from default dataset. See val_lab.
  • .fre Simple frequencies of variable in the default data.frame. See fre.
  • .cro/.cro_cpct/.cro_rpct/.cro_tpct Simple crosstabulations of variable in the default data.frame. See cro.
  • .cro_mean/.cro_sum/.cro_median/.cro_fun/.cro_fun_df Simple crosstabulations of variable in the default data.frame. See cro_fun.
  • .with Evaluate arbitrary expression in the context of data.frame. See with.

Examples

Run this code
data(mtcars)

default_dataset(mtcars) # set mtcars as default dataset

# calculate new variables
.compute({
    mpg_by_am = ave(mpg, am, FUN = mean)
    hi_low_mpg = ifs(mpg<mean(mpg) ~ 0, default = 1)    
})

# set labels
.set_var_lab(mpg, "Miles/(US) gallon")
.set_var_lab(cyl, "Number of cylinders")
.set_var_lab(disp, "Displacement (cu.in.)")
.set_var_lab(hp, "Gross horsepower")
.set_var_lab(mpg_by_am, "Average mpg for transimission type")
.set_var_lab(hi_low_mpg, "Miles per gallon")
.set_val_lab(hi_low_mpg, ml_left("
                                  0 Low
                                  1 High
                                  "))

.set_var_lab(vs, "Engine")
.set_val_lab(vs, ml_left(" 
                          0 V-engine
                          1 Straight engine
                          "))

.set_var_lab(am, "Transmission")
.set_val_lab(am, ml_left(" 
                          0 automatic
                          1 manual
                          "))

# calculate frequencies
.fre(hi_low_mpg)
.cro(cyl, hi_low_mpg)
.cro_mean(mpg, am)
.cro_mean(data.frame(mpg, disp, hp), vs)

# disable default dataset
default_dataset(NULL)

# Example of .recode

data(iris)

default_dataset(iris) # set iris as default dataset

.recode(Sepal.Length, lo %thru% median(Sepal.Length) ~ "small", other ~ "large")

.fre(Sepal.Length)

# example of .do_if
 
.do_if(Species == "setosa",{
     Petal.Length = NA
     Petal.Width = NA
})

.cro_mean(data.frame(Petal.Length, Petal.Width), Species)

# disable default dataset
default_dataset(NULL)

Run the code above in your browser using DataLab