
Last chance! 50% off unlimited learning
Sale ends in
modify
evaluates expression expr
in the context of data.frame
data
. It works similar to within
in base R but try
to return new variables in order of their appearance in the expression.
modify_if
modifies only rows for which cond
has TRUE. Other
rows remain unchanged. Newly created variables also will have values only in
rows for which cond
has TRUE. There will be NA's in other rows. This
function tries to mimic SPSS "DO IF(). ... END IF." statement. There is a
special constant .n
which equals to number of cases in data
for
usage in expression inside modify
. Inside modify_if
.n
gives number of rows which will be affected by expressions. Inside these
functions you can use set
function which creates variables with given
name/set values to existing variables - .set. It is possible with
set
to assign values to multiple variables at once.
modify(data, expr)
modify_if(data, cond, expr)
data
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