R aggregate list of dataframe

烈酒焚心 提交于 2019-12-12 04:15:55

问题


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.


回答1:


## 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) })


来源:https://stackoverflow.com/questions/40567653/r-aggregate-list-of-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!