How do you pivot data from a list of data frames in R?

前端 未结 3 1726
野趣味
野趣味 2021-01-25 05:22

Let\'s say I have a list of data frames ldf:

df1 <- data.frame(date = c(1,2), value = c(4,5))
df2 <- data.frame(date = c(1,2), value = c(4,5))
ldf <- li         


        
相关标签:
3条回答
  • 2021-01-25 05:56

    Another option is to use unnest from "tidyr" in conjunction with the typical grouping and aggregation functions via "dplyr":

    library(dplyr)
    library(tidyr)
    
    unnest(ldf) %>%
      group_by(date) %>%
      summarise(value = sum(value))
    # Source: local data frame [2 x 2]
    # 
    #   date value
    # 1    1     8
    # 2    2    10
    
    0 讨论(0)
  • 2021-01-25 06:04

    If these rows were all in the same data frame, you would use aggregate to do the sum. You can combine them with rbind so they are in the same data frame:

    aggregate(value ~ date, data=do.call(rbind, ldf), FUN=sum)
      date value
    1    1     8
    2    2    10
    

    If the date columns in all the data frames are identical, you can easily use Reduce to do the sum:

    Reduce(function(x, y) data.frame(date=x$date, value=x$value+y$value), ldf)
      date value
    1    1     8
    2    2    10
    

    This should be a lot faster than rbind-ing the data together and aggregating.

    0 讨论(0)
  • 2021-01-25 06:14

    You could use:

    library(data.table)
    dt1 <- rbindlist(ldf)
    setkey(dt1,'date')
    dt1[,list(value=sum(value)), by='date']
     date value
    1:    1     8
    2:    2    10
    
    0 讨论(0)
提交回复
热议问题