Learn R Programming

expss (version 0.10.7)

vars: Get variables/range of variables by name/by pattern.

Description

  • vars returns data.frame with all variables by their names or by criteria (see criteria). There is no non-standard evaluation in this function by design so use quotes for names of your variables. This function is intended to get variables by parameter/criteria. The only exception with non-standard evaluation is %to%. You can use %to% inside vars or independently.

  • ..p returns data.frame with all variables which names satisfy supplied perl-style regular expression. Arguments for this function is quoted characters. It is a shortcut for vars(perl(pattern)).

  • ..f returns data.frame with all variables which names contain supplied pattern. Arguments for this function can be unquoted. It is a shortcut for vars(fixed(pattern)).

  • ..t returns data.frame with variables which names are stored in the supplied arguments. Expressions in characters in curly brackets are expanded. See text_expand.

  • ..[] returns data.frame with all variables by their names or by criteria (see criteria). Names at the top-level can be unquoted (non-standard evaluation). For standard evaluation of parameters you can surround them by round brackets. You can assign to this expression. If there are several names inside square brackets then each element of list/data.frame from right side will be assigned to appropriate name from left side. You can use item1 %to% item2 notation to get/create sequence of variables. If there are no arguments inside square brackets than from each item of RHS will be created separate variable in the parent frame. In this case RHS should be named list or data.frame.

  • ..$name sets/returns object which name is stored in the variable name. It is convenient wrapper around get/assign functions.

  • %to% returns range of variables between e1 and e2 (similar to SPSS 'to'). modify, modify_if, calculate, keep, except and where support %to%.

  • indirect/indirect_list are aliases for vars/vars_list.

Functions with word 'list' in name return lists of variables instead of dataframes.

Usage

vars(...)

vars_list(...)

indirect(...)

indirect_list(...)

e1 %to% e2

e1 %to_list% e2

..

..f(...)

..p(...)

..t(...)

Arguments

...

characters names of variables or criteria/logical functions

e1

unquoted name of start variable (e. g. a_1)

e2

unquoted name of start variable (e. g. a_5)

Value

data.frame/list with variables

Format

An object of class parameter of length 1.

See Also

keep, except, do_repeat, compute, calculate, where

Examples

Run this code
# NOT RUN {
# In data.frame
dfs = data.frame(
    a = rep(1, 5),
    b_1 = rep(11, 5),
    b_2 = rep(12, 5),
    b_3 = rep(13, 5),
    b_4 = rep(14, 5),
    b_5 = rep(15, 5) 
)

# calculate sum of b_* variables
compute(dfs, {
    b_total = sum_row(b_1 %to% b_5)
})

# identical result
compute(dfs, {
    b_total = sum_row(..f(b_))
})

compute(dfs, {
    b_total = sum_row(..t("b_{1:5}"))
})

# In global environement
a = rep(10, 5)
a1 = rep(1, 5)
a2 = rep(2, 5)
a3 = rep(3, 5)
a4 = rep(4, 5)
a5 = rep(5, 5)

# identical results
a1 %to% a5
vars(perl("^a[0-9]$"))
..[perl("^a[0-9]$")]
..p("^a[0-9]$")
..t("a{1:5}")

# sum each row
sum_row(a1 %to% a5)

# variable substitution
name1 = "a"
name2 = "new_var"

# in global environment
..$name1 # give as variable 'a'

..$name2 = ..$name1 * 2 # create variable 'new_var' which is equal to 'a' times 2
new_var

# inside data.frame
compute(dfs, {
     ..$name2 = ..$name1*2    
})

compute(dfs, {
     for(name1 in paste0("b_", 1:5)){
         name2 = paste0("new_", name1) 
         ..$name2 = ..$name1*2 
     }
     rm(name1, name2) # we don't need this variables as columns in 'dfs'
})

# square brackets notation - multi-assignment
name1 = paste0("b_", 1:5)
compute(dfs, {
          # round brackets about 'name1' is needed to avoid using it 'as is'
         ..[paste0("new_", name1)] = ..[(name1)]*2  
})

# the same result
# note the automatic creation of sequence of variables
compute(dfs, {
         ..[new_b_1 %to% new_b_5] = ..[b_1 %to% b_5]*2  
})

# assignment form of 'recode' on multiple variables
compute(dfs, {
         recode(..[b_1 %to% b_5]) = 13 %thru% hi ~ 20   
})

# empty brackets - unboxing of dichotomy.
compute(dfs, {
         ..[] =  as.dichotomy(b_1 %to% b_5, prefix = "v_")   
})
# }

Run the code above in your browser using DataLab