问题
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))
})
回答1:
Using purrr
's map
, you can apply the chunk of code you specified to all the columns that are character. Basically you map the names of character variables to the function that follows
purrr::map(names(df %>% select(where(is.character))), function(i) {
df %>%
group_by(!!sym(i)) %>%
summarize(newValue = mean(A))
})
Output
# [[1]]
# A tibble: 3 x 2
# B newValue
# <chr> <dbl>
# 1 Group 1 7
# 2 Group 2 3.5
# 3 Group 3 4
#
# [[2]]
# A tibble: 3 x 2
# C newValue
# <chr> <dbl>
# 1 Group 1 5
# 2 Group 2 8.5
# 3 Group 3 1.5
回答2:
You can pivot it long, using A as an identifier, and then group by:
library(tidyr)
df %>% pivot_longer(-A) %>% group_by(name,value) %>% summarize(val=mean(A))
# A tibble: 6 x 3
# Groups: name [2]
name value val
<chr> <fct> <dbl>
1 B Group 1 7
2 B Group 2 3.5
3 B Group 3 4
4 C Group 1 5
5 C Group 2 8.5
6 C Group 3 1.5
回答3:
You could try something like this:
library(dplyr)
empty_list <- list(0)
for(i in 2:dim(df)[2])
{
empty_list[[i-1]]<-df %>% group_by(df[,i]) %>% summarise(val = mean(A))
}
empty_list
[[1]]
# A tibble: 3 x 2
`df[, i]` val
<fct> <dbl>
1 Group 1 7
2 Group 2 3.5
3 Group 3 4
[[2]]
# A tibble: 3 x 2
`df[, i]` val
<fct> <dbl>
1 Group 1 5
2 Group 2 8.5
3 Group 3 1.5
Hope this can help.
来源:https://stackoverflow.com/questions/62471927/creating-a-dynamic-group-by