Learn R Programming

expss (version 0.10.5)

experimental: 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

.compute(expr)

.do_if(cond, expr)

.modify_if(cond, expr)

.modify(expr)

.calculate(expr, use_labels = FALSE)

.calc(expr, use_labels = FALSE)

.val_lab(...)

.var_lab(...)

.recode(x, ...)

.fre(...)

.cro(...)

.cro_cases(...)

.cro_cpct(...)

.cro_rpct(...)

.cro_tpct(...)

.cro_mean(...)

.cro_sum(...)

.cro_median(...)

.cro_mean_sd_n(...)

.cro_fun(...)

.cro_fun_df(...)

Arguments

expr

set of expressions in curly brackets which will be evaluated in the context of default dataset

cond

logical vector/expression

use_labels

logical. Experimental feature. If it equals to TRUE then we will try to replace variable names with labels. Many base R functions which show variable names will show labels.

...

further arguments

x

vector/data.frame - variable names in the scope of default dataset

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.

  • .recode Change, rearrange or consolidate the values of an existing variable inside default data.frame. See recode.

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.

  • .calculate Evaluate arbitrary expression in the context of data.frame. See calculate.

Examples

Run this code
# NOT RUN {
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, TRUE ~ 1)    
})

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

    vs = "Engine",
    vs = num_lab(" 
                     0 V-engine
                     1 Straight engine
                 "),

    am = "Transmission",
    am = num_lab(" 
                     0 Automatic
                     1 Manual
                          ")
)
# calculate frequencies
.fre(hi_low_mpg)
.cro(cyl, hi_low_mpg)
.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