Home > Net >  R function to aggregate all elements in a list
R function to aggregate all elements in a list

Time:11-17

Using the tidyverse package, I can easily aggregate a single variable. However, I wish to create a function which will allow me to aggregate multiple variables simultaneously.

I understand I have to convert the dataframe containing multiple variables to a list and then lapply an aggregating function across this list. However, I am unable to create this function.

Following is a REPREX of what I am trying to do:

# Load package

library(dplyr)

# Load dataset

dat <- data.frame(Titanic)

# Select variables

dat <- dat[, c('Class', 'Sex', 'Age','Survived')]

# Aggregate a single variable

dat %>% group_by(Class) %>% summarise(n=n())
        
# Desired outcome: Aggregate all variables simultaneously using a function

dat_ls <- as.list(dat)                       ## Create a list with all the variables
dat_agg <- lapply(dat_ls, function(???))     ## Apply aggregating function to each element in the list

CodePudding user response:

With the list, we can use table

lapply(dat_ls, table)

Another option is to reshape to 'long' format and then use count

library(dplyr)
library(tidyr)
dat %>% 
   pivot_longer(everything()) %>% 
   count(name, value)
  • Related