Aggregate variables in list of data frames into single data frame

后端 未结 2 1964
野趣味
野趣味 2021-01-07 07:03

I am performing a per policy life insurance valuation in R. Monthly cash flow projections are performed per policy and returns a data frame in the following format (for exam

2条回答
  •  温柔的废话
    2021-01-07 07:18

    I think this solution might be efficient. Give it a try and let me know

    require(data.table)
    newagg <- function(dataset) { dataset <- data.table(dataset);dataset <- dataset[,lapply(.SD,sum),by=ProjM,.SDcols=c("Cashflow1","Cashflow2")]; return(dataset)}
    newagg(rbindlist(lapply(ListOfDataFrames,newagg)))
    # ProjM Cashflow1 Cashflow2
    # 1:     1        55       -35
    # 2:     2        55       -35
    # 3:     3        55       -35
    # 4:     4         5        10
    # 5:     5         5        10
    

提交回复
热议问题