cars %>%
group_by(cyl) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
I want to turn the above code into a function, where the dataframe (cars) and the column (cyl) are arguments. How can I do this in R?
I tried the following below but this does not evaluate
plot_cars <- function (df, col) {
df %>%
group_by(col) %>%
summarise_each(funs(mean(., na.rm = TRUE),
min(., na.rm = TRUE),
max(., na.rm = TRUE),
sd(., na.rm = TRUE)),
mpg, wt)
}
plot_cars(cars,"cyl")