0

I can't figure out how to loop when there is data masking going on (aka, it is trying to recognize my looping variable as a column in the dataset.

The current issue is that {{var_i}} is printing as "age" but the group_by needs it to be just age

test <- c("age", "year")
   
for (var_i in test) {
  df_explore <- df %>%
    group_by( {{var_i}} ) %>%
    summarize(rate = sum(y) / sum(x))

  ggplot(df_explore, aes(x = {{var_i}}, y = rate) + 
    geom_line() + 
    labs(x = {{var_i}}, y = "Rate")
}   
MrFlick
  • 178,638
  • 15
  • 253
  • 268
  • Rather than `{{var_i}}` use `.data[[var_i]]`. The `.data` pronoun is the recommended way to use string column names with ggplot and dplyr. – MrFlick Aug 05 '21 at 18:18

0 Answers0