Guil Botler Guil Botler - 25 days ago 7
R Question

R aggregate list of dataframe

I do have a list of 53 data frames (purchase01 to purchase53), ordered by date, with 18 variables and distinct number of rows (tryed, but coulden't paste a example on the following). I want to aggregate each distinct data frame by its repeated values of "V9"- factor- by sum the column "V2"- numeric. I coulden't find the answer yet.

For only one dataframe I can use
aggregate.data.frame(purchase00$V12, by = list(purchase00($V9),FUN = sum) and itjust works fine.

I tryed llply,
llply(.data = purchase, .fun = aggregate.data.frame, by= list(unique((V9),sum, .inform = TRUE)

but without sucess, it seems that the function does not receive the by for every dataframe nor the function sum. Also the mapply didn't work well for me.

thank you in advance, I apreciate any help.

Answer
## assuming your list is called list_of_df
require(dplyr)

summarized_list <- lapply(list_of_df,  function(x) {

x %>% group_by(V9) %>% summarize(sum(V2))

})

This will return a list with the summary of each data frame in your purchase list.

If you don't want to use dplyr , you can do this with base :

lapply(list_of_df, function(x) {aggregate(values ~ day_of_week, data = x, sum) })