I need help in dynamically adding up variables and evaluating them inside a dplyr pipe.
For example I have a dataset and I have to go:
test_variable_construction %>%
mutate(Total_Utilisation_Sum_3m = Total_Utilisation Lag1_Total_Utilisation Lag2_Total_Utilisation)
and
test_variable_construction %>%
mutate(Total_Utilisation_Sum_6m = Total_Utilisation Lag1_Total_Utilisation Lag2_Total_Utilisation Lag3_Total_Utilisation Lag4_Total_Utilisation Lag5_Total_Utilisation)
so I want to create a string as such:
paste(iterates,collapse=" ")
"Total_Utilisation Lag1_Total_Utilisation Lag2_Total_Utilisation"
and pass this into the dplyr pipe:
test_variable_construction %>%
mutate(Total_Utilisation_Sum_3m := eval(!!paste(iterates,collapse=" "))
Unfortunately, that doesn't work -- does anyone know the right syntax to for this?
CodePudding user response:
One option to achieve your desired result would be to use rowwise like so:
library(dplyr)
test_df <- data.frame(Total = 1:5)
test_df[paste0("Lag", 1:2, "_Total")] <- lapply(1:2, function(x) 1:5)
iterates <- c("Total", paste0("Lag", 1:2, "_Total"))
test_df %>%
rowwise() %>%
mutate(Total_Sum_3m = sum(c_across(all_of(iterates)))) %>%
ungroup()
#> # A tibble: 5 × 4
#> Total Lag1_Total Lag2_Total Total_Sum_3m
#> <int> <int> <int> <int>
#> 1 1 1 1 3
#> 2 2 2 2 6
#> 3 3 3 3 9
#> 4 4 4 4 12
#> 5 5 5 5 15
Or using rowSums you could do:
test_df %>%
mutate(Total_Sum_3m = rowSums(.[iterates]))
#> Total Lag1_Total Lag2_Total Total_Sum_3m
#> 1 1 1 1 3
#> 2 2 2 2 6
#> 3 3 3 3 9
#> 4 4 4 4 12
#> 5 5 5 5 15
Note: Using eval you could achieve your result via
test_df %>%
mutate(Total_Sum_3m = eval(parse(text = paste(iterates, collapse = " "))))
However, in general using eval(parse = ... is not recommended. See e.g. this post for a collection of reasons to avoid eval(parse = ....
CodePudding user response:
So the parse_expr from rlang worked for me.
For example, I had to go:
test_variable_construction %>%
mutate(Total_Utilisation_Sum_3m = !!parse_expr(paste(iterates), collapse = " "))
