如何将第n列与数据帧中的第n + 1列相加

时间:2015-02-25 13:45:46

标签: r function dataframe sum multiple-columns

如何在列的子集的数据框中将 n 列与 n-1 列相加?

例如,我有一个如下数据框:

ID          C1  C2  C3 
1   2000-12-24  3d  2d
2   2000-12-24  2d  1d

我希望R执行以下操作:

ID C1         C2                        C3           
1  2000-12-24 2000-12-24+3d=2000-12-27  2000-12-27+2d=2000-12-29
2  2000-12-24 2000-12-24+2d=2000-12-26  2000-12-26+1d=2000-12-27

这样最终的数据框如下所示:

ID  C1          C2          C3          ...
1   2000-12-24  2000-12-27  2000-12-29
2   2000-12-24  2000-12-26  2000-12-27

更新:

已相应生成数据:

library(plyr)
library(lubridate)
library(reshape2)
Heterotransaction <- rgamma(2,shape=3 , scale=1)
ID <- list(1:2) 
Elog <- data.frame(ID,Heterotransaction)
Elog$fist_transaction <- "2000-12-24"
Elog$fist_transaction <- as.Date(Elog$fist_transaction, "%Y-%m-%d")
Heterotransaction <- rgamma(2,shape=3 , scale=1) 

f.transaction <- function(x){
  y<- (rexp(2,x))
duration(y, units = "years")
}

tbtrans<-ldply(Heterotransaction, f.transaction) 
purchases<-data.frame(ID,tbtrans) 
Elognew<- merge.data.frame(Elog, purchases)

1 个答案:

答案 0 :(得分:1)

你可以尝试

 df1[3:ncol(df1)] <- lapply(3:ncol(df1), function(i) rowSums(df1[2:i])) 
 df1
 #  ID C1 C2 C3
 #1  1  2  5  7
 #2  2  4  7  8

 df1[-1] <- t(apply(df1[-1], 1, cumsum))

或另一种选择是使用Reduce

 library(data.table)
 setDT(df1)[,2:ncol(df1) := Reduce(`+`, .SD, accumulate=TRUE),
                       .SDcols=2:ncol(df1)][]
 #   ID C1 C2 C3
 #1:  1  2  5  7
 #2:  2  4  7  8

更新

基于新数据集,一个选项是修改第一个解决方案

 df2[3:ncol(df2)] <- do.call(rbind, lapply(3:ncol(df2), function(i) 
     as.Date(df2[,2]+cumsum(as.numeric(sub('[^0-9]+', '', df2[,i]))))))
 df2[3:ncol(df2)] <- lapply(df2[3:ncol(df2)], as.Date, origin='1970-01-01')

 df2
 # ID         C1         C2         C3
 #1  1 2000-12-24 2000-12-27 2000-12-29
 #2  2 2000-12-24 2000-12-26 2000-12-27

数据

df1 <- structure(list(ID = 1:2, C1 = c(2L, 4L), C2 = c(3L, 3L), 
C3 = c(2L, 1L)), .Names = c("ID", "C1", "C2", "C3"),
 class = "data.frame", row.names = c(NA, -2L))

df2 <- df2 <- structure(list(ID = 1:2, C1 = structure(c(11315, 11315),
class = "Date"), 
C2 = c("3d", "2d"), C3 = c("2d", "1d")), .Names = c("ID", 
"C1", "C2", "C3"), row.names = c(NA, -2L), class = "data.frame")