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?
lapply..data[[resp]]instead ofresp. Certainly replacingresponsewith.data[[response]]in the second example does what you want. Alternatively you can use!!str2lang(response)require(), and there’s also no need to usedata()(here, or almost anywhere these days; see R packages chapter 7.1: Exported data (in the “Warning” box)).