I'd like to make a function in which I can supply a dataset and a list of prefixes. It uses those prefixes to construct columns that involve the use of those prefixes, and returns the final dataset, like so:
data <- data.frame(demo1_a = 3,demo1_b = 5, demo2_a = 4,demo2_b = 5)
make_vars(data, prefixes = list('demo1', 'demo2'))
make_vars <- function(data, prefixes){
for(i in prefixes){
data = data %>%
mutate({i}_subtraction = {i}_a - {i}_b,
{i}_addition = {i}_a + {i}_b,
)
}
return(data)
}
The result data.frame would look like:
demo1_a demo1_b demo2_a demo2_b demo1_subtraction demo1_addition demo2_addition demo2_subtraction
1 3 5 4 5 -2 8 9 -1
I see in this answer: https://stackoverflow.com/a/59224230/372526, they reference curly-curly operators ({{}}) in rlang
which allows you to reference a variable name dynamically, but I haven't found how to "paste" other suffixes like "_subtraction" and "_b" to that through a function.