dfs = data.frame(
test = 1:5,
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_, b_1 %to% b_5)
var_lab(b_total) = "Sum of b"
random_numbers = runif(.n) # .n usage
})
# 'set' function
# new variables filled with NA
modify(dfs, {
set('new_b`1:5`')
})
# 'set' function
# set values to existing/new variables
# expression in backticks will be expanded - see ?subst
modify(dfs, {
set('new_b`1:5`', b_1 %to% b_5)
})
# conditional modification
modify_if(dfs, test %in% 2:4, {
aa = aa + 1
a_b = aa + b_
b_total = sum_row(b_, b_1 %to% b_5)
random_numbers = runif(.n) # .n usage
})
Run the code above in your browser using DataLab