0

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

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 3
    It's generally bad practice to name variables after oft-used base R functions. While not a syntax error (and R usually knows what you intend), I recommend against using `list` as a variable name. – r2evans Sep 21 '21 at 14:00

2 Answers2

1

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

Quixotic22
  • 2,894
  • 1
  • 6
  • 14
  • As @RuiBarradas points out below, this overwrites the `df1` data frame on each iteration of the loop, which is unlikely to be OP's intent. – Limey Sep 21 '21 at 14:13
0

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.
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • 2
    Also note the [`.data` pronoun](https://rlang.r-lib.org/reference/tidyeval-data.html), which can be useful when working with strings and tidy evaluation. E.g., `sum(.data[[i]])` – aosmith Sep 21 '21 at 15:44
  • @aosmith You could post your comment as an answer, imho that's precisely what the question asks for, what to put in `sum` when the summand is given as a character variable. – Rui Barradas Sep 21 '21 at 17:10