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