OBoro
OBoro

Reputation: 79

dplyr group_by loop through different columns

I have the following data;

enter image description here

I would like to create three different dataframes using group_by and summarise dplyr functions. These would be df_Sex, df_AgeGroup and df_Type. For each of these columns I would like to perform the following function;

 df_Sex =  df%>%group_by(Sex)%>%summarise(Total = sum(Number))

Is there a way of using apply or lapply to pass the names of each of these three columns (Sex, AgeGrouping and Type) to these create 3 dataframes?

Upvotes: 0

Views: 746

Answers (2)

George
George

Reputation: 903

This will work but will create a list of data frames as your output

### Create your data first    

df <- data.frame(ID = rep(10250,6), Sex = c(rep("Female", 3), rep("Male",3)), 
                     Population = c(rep(3499, 3), rep(1163,3)), AgeGrouping =c(rep("0-14", 3), rep("15-25",3)) , 
                     Type = c("Type1", "Type1","Type2", "Type1","Type1","Type2"), Number = c(260,100,0,122,56,0))

gr <- list("Sex", "AgeGrouping","Type")

df_list <- lapply(gr, function(i) group_by(df, .dots=i) %>%summarise(Total = sum(Number)))

Upvotes: 2

josemz
josemz

Reputation: 1312

Here's a way to do it:

f <- function(x) {
     df %>% 
         group_by(!!x) %>% 
         summarize(Total = sum(Number))
}

lapply(c(quo(Sex), quo(AgeGrouping), quo(Type)), f)

There might be a better way to do it, I haven't looked that much into tidyeval. I personally would prefer this:

library(data.table)
DT <- as.data.table(df)
lapply(c("Sex", "AgeGrouping", "Type"), 
       function(x) DT[, .(Total = sum(Number)), by = x])

Upvotes: 1

Related Questions