I would like to create a loop that would run over a list of strings. This list of strings contains name of the variables that need to be plugged-in the syntax. My syntax sometimes need them with quotes (and then it works) and sometimes without quotes (and then it fails). I have tried solutions from here Remove quotes from a character vector in R but it didn't work.
here is an example of what I try to do:
library(dplyr)
data(mtcars)
list=c("mpg", "cyl")
for (i in list) {
print(i)
df=mtcars[,c(i, "gear")] #this works
df1 = group_by(df, gear) %>% summarize(Y=sum(i)) # this doesn't work - it doesn't recognize i as variable name
}
I'd appreciate your help
Regards, Polina
CodePudding user response:
Try wrapping i
in a get
.
for (i in list) {
print(i)
df=mtcars[,c(i, "gear")] #this works
df1 = group_by(df, gear) %>% summarize(Y=sum(get(i))) # this doesn't work - it doesn't recognize i as variable name
}
It's also worth reading about sym
and !!
etc
CodePudding user response:
To correct the error of accessing the variable given by a character variable, use !!rlang::sym(i)
.
for (i in list) {
print(i)
df=mtcars[,c(i, "gear")] #this works
df1 = group_by(df, gear) %>% summarize(Y = sum(!!rlang::sym(i)))
}
But there is another error in the question's code, the output df1
is rewritten every time through the loop.
Here is a corrected loop.
df_list <- lapply(list, function(i){
print(i)
df <- mtcars[,c(i, "gear")] #this works
group_by(df, gear) %>% summarize(Y = sum(!!rlang::sym(i)))
})
df_list[[1]]
## A tibble: 3 x 2
# gear Y
# <dbl> <dbl>
#1 3 242.
#2 4 294.
#3 5 107.