Learn R Programming

expss (version 0.7.1)

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

Description

  • vars returns all variables by their names or by criteria (see criteria). Expressions in backticks inside characters will be expanded as with subst. a`1:2` will be translated to 'a1', 'a2'. There is no non-standard evaluation in this function by design so use quotes for names of your variables or use qc. The only exception with non-standard evaluation is %to%. You can use %to% inside vars or independently.

  • %to% returns range of variables between e1 and e2 (similar to SPSS 'to'). modify, modify_if, calculate, keep, except and where support %to%. Inside global environment with, within %to% will take range from names of variables sorted in the alphabetic order.

Functions with word 'list' in name return lists of variables instead of dataframes. vars_pattern, vars_pattern_list, vars_range and vars_range_list are deprecated and will be removed in the future version. .internal_to_ is for internal usage and not documented.

Usage

vars(...)

vars_list(...)

e1 %to% e2

e1 %to_list% e2

.internal_to_(e1, e2)

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

See Also

keep

Examples

Run this code
# NOT RUN {
# In data.frame
dfs = data.frame(
    aa = rep(10, 5),
    b_ = rep(20, 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
modify(dfs, {
    b_total = sum_row(b_1 %to% b_5)
    b_total2 = sum_row(vars("b_`1:5`"))
})

# In global environement
aa = rep(10, 5)
b = rep(20, 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("a`1:5`")
vars(perl("^a[0-9]$"))

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

# }

Run the code above in your browser using DataLab