Christopher Penn
Christopher Penn

Reputation: 539

Aggregate dataframes in a list by date and then merge back - lapply?

I'm struggling to figure out how to aggregate and then merge a list of dataframes. Has anyone done this?

Here's the example. I have a list of dataframes created by splitting on a categorical variable.

list1df 93 obs of 6 variables:
- categoricalvar1: Factor w...
- categoricalvar2: Factor w...
- categoricalvar3: Factor w...
- numericvar1: num...
- numericvar2: num...
- thedate: Date, format "2018-11-13"...

list2df 3988 obs of 6 variables:
- categoricalvar1: Factor w...
- categoricalvar2: Factor w...
- categoricalvar3: Factor w...
- numericvar1: num...
- numericvar2: num...
- thedate: Date, format "2018-11-13"...

list3df 563 obs of 6 variables:
- categoricalvar1: Factor w...
- categoricalvar2: Factor w...
- categoricalvar3: Factor w...
- numericvar1: num...
- numericvar2: num...
- thedate: Date, format "2018-11-13"...

I'm trying to figure how to lapply/sapply something like this:

sumtable<- function(thedate,df,thefun)
{
  dfgrouped <- aggregate(. ~thedate, data=df, thefun, na.rm=TRUE)
  return(dfgrouped)
}

to each individual dataframe in the list, to create aggregated frames by individual day, after which I'll use ldply(thebiglist,data.frame) to glue them all back together.

I can't figure out how to make the sumtable function work with lapply and all the frames in the list though. Thank you in advance!

Upvotes: 1

Views: 78

Answers (1)

akrun
akrun

Reputation: 887891

We can place the data.frames in a list and use aggregate with the specific function

lst <- lapply(mget(ls(pattern = '^list\\d+df$')), function(x) 
    aggregate(. ~ thedate, data = x, FUN = max, na.rm = TRUE))

do.call(rbind, lst)

Upvotes: 1

Related Questions