Say I have a data frame like this in R:
df <- data.frame(factor1 = c("A","B","B","C"),
factor2 = c("M","F","F","F"),
factor3 = c("0", "1","1","0"),
value = c(23,32,4,1))
I want to get a summary statistic in dplyr
grouped by one variable, like so (but more complicated):
df %>%
group_by(factor1) %>%
summarize(mean = mean(value))
Now I'd like to do this for all factor columns (think 100 factor variables). Is there a way to do this within dplyr? I was also thinking of doing a for
loop over names(df)
but I get the variables as strings and group_by()
doesn't accept strings.