df = data.frame(
A = c(1, 4, 5, 13, 2),
B = c("Group 1", "Group 3", "Group 2", "Group 1", "Group 2"),
C = c("Group 3", "Group 2", "Group 1", "Group 2", "Group 3")
)
df %>%
group_by(B) %>%
summarise(val = mean(A))
df %>%
group_by(C) %>%
summarise(val = mean(A))
Instead of writing a new chunck of code for each unique set of group_by I would like to create a loop that would iterate through the df data frame and save the results into a list or a data frame.
I would like to see how the average value of feature A is spread acorss features B and C, without having to write a new chunck of code for each categorical feature in the data set.
I tried this:
List_Of_Groups <- map_df(df, function(i) {
df %>%
group_by(!!!syms(names(df)[1:i])) %>%
summarize(newValue = mean(A))
})