# 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
vars_range("a1", "a5")
a1 %to% a5
vars("a`1:5`")
vars_pattern("^a[0-9]$")
# sum each row
sum_row(a1 %to% a5)
# 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)
)
# all variables that starts with 'b'
with(dfs, vars_pattern("^b"))
# calculate sum of b_* variables
modify(dfs,{
b_total = sum_row(b_1 %to% b_5)
b_total2 = sum_row(vars("b_`1:5`"))
})
Run the code above in your browser using DataLab