Consider I have a dataframe, and want to compute the sum of two random columns.
I tried:
df <- data.frame(a=seq(1,3),b=seq(4,6),c=seq(7,9))
vars <- sample(letters[1:3],2)
df %>% rowwise() %>% mutate(s=sum({{vars}}))
But I get:
> df %>% rowwise() %>% mutate(s=sum({{vars}}))
Error in `.fun()`:
ℹ In argument: `s = sum(c("c", "b"))`.
ℹ In row 1.
Caused by error in `sum()`:
! invalid 'type' (character) of argument
Run `rlang::last_trace()` to see where the error occurred.
Because, although the vector is expanded, the strings inside it are not converted to symbols.
What would be the tidyverse way to do that?
dplyr, I can't find an exact dupe. If someone can I'm happy to delete my answer and link to that.