0

I'm trying to loop over a list of column names from a dataframe, and compute a summary table. I can't get the variable names to work as parameters for the functions.

Here's an example of what I want:

## MWE:
require(dplyr)
data(mtcars)
## str(mtcars)

## Example of overall goal (doesn't work):
for(resp in c("mpg", "disp", "hp")) {
    mtcars |>
        group_by(cyl) |>
        summarize(n = length(resp),
                  avg = mean(resp),
                  s = sd(resp))
}

## This works:
mtcars |>
    group_by(cyl) |>
    summarize(avg = mean(mpg))

## This doesn't:
response <- "mpg"
mtcars |>
    group_by(cyl) |>
    summarize(avg = mean(response))

How can I use a variable as the parameter of a function?

10
  • 1
    Have a look: dplyr.tidyverse.org/articles/programming.html This is aggregate task, e.g. stackoverflow.com/questions/12064202/… + lapply. Commented May 25 at 15:53
  • 2
    Your loop doesn't actually do anything, since you are not returning anything from it, but essentially you can use .data[[resp]] instead of resp. Certainly replacing response with .data[[response]] in the second example does what you want. Alternatively you can use !!str2lang(response) Commented May 25 at 16:01
  • 1
    As an aside: Never use require(), and there’s also no need to use data() (here, or almost anywhere these days; see R packages chapter 7.1: Exported data (in the “Warning” box)). Commented May 25 at 16:06
  • Welcome to the wonderful world of NSE in the tidyverse. Commented May 25 at 17:30
  • Wow. Thank you all. I never heard of NSE in R before. Thanks for pointing this out. Commented May 25 at 17:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.