I am having a bit of difficulty understanding how to pass a column from a nested tibble into a function argument. As an example, the following code return the mean of "cyl" grouped by "am":
test <- mtcars %>%
group_by(am) %>%
nest()
get_mean <- function (df) {
return (mean(df$cyl))
}
test <- test %>%
mutate(mean = map_dbl(data, get_mean))
But what I wanted the mean of a column other than cyl, and wanted to pass that into the function as an argument? I know this is wrong code, but I would try to write something like this:
test <- mtcars %>%
group_by(am) %>%
nest()
get_mean <- function (df, column) {
return (mean(df${{column}}))
}
test <- test %>%
mutate(mean = map_dbl(data, get_mean, column))
Any help around this would be appreciated. How would I get column
into the map function and how am I supposed to write df${{column}}
?