I want to summarize the data by treatments for all the variables I have, and put them together, so I define a function to do this for each variable first, and then rbind them later on.
SummarizeFn <- function(x,y) {
x %>% group_by(Treatment) %>%
summarize(
n = n(),
Mean = mean(y),
SD = sd(y)
) %>% cbind ("Var" = rep(y, 3)) # add a column to show which variable those statistics belong to.
}
SumPrice <- SummarizeFn(df, Price)
However, I have two problems here.
First, R tells me that Error in mean(y): can't find object 'Price'.
Second, in the last part, cbind(), how can I pass the name of the variable, Price for example, as a character string in that column? Double quote is not going to work for sure, I also tried paste function which does not work either. How to do this?