如何计算与先前数据的累积数据差异

时间:2017-08-02 17:12:29

标签: r for-loop cumulative-sum

我想要的结果就像下面的cum_diff_preceding cloumn。

data    cum_diff_preceding
2016/1/10   0
2016/2/4    25
2016/3/25   125
2016/4/13   182
2016/5/5    270

for row 2016/2/4, cum_diff_preceding is (2016/2/4-2016/1/10)
for row 2016/3/25, cum_diff_preceding is (2016/3/25-2016/1/10)+(2016/3/25-2016/2/4)
for row 2016/4/13, cum_diff_preceding is (2016/4/13-2016/1/10)+(2016/4/13- 2016/2/4)+(2016/4/13-2016/3/25)
……

是否需要循环以及代码是什么?非常感谢

更多,如果我想按小组处理上述内容,我该怎么办呢?

   data group
    2016/1/10   1
    2016/2/4    1
    2016/3/25   1
    2016/4/13   1
    2016/5/5    1
    2016/7/1    2
    2016/8/1    2
    2016/10/1   2
    2016/12/1   2
    2016/12/31  2
for row 2016/1/10, cum_diff_preceding is 0
for row 2016/2/4, cum_diff_preceding is (2016/2/4-2016/1/10)
for row 2016/3/25, cum_diff_preceding is (2016/3/25-2016/1/10)+(2016/3/25-2016/2/4)
for row 2016/4/13, cum_diff_preceding is (2016/4/13-2016/1/10)+(2016/4/13- 2016/2/4)+(2016/4/13-2016/3/25)
for row 2016/5/5, cum_diff_preceding is (2016/5/5-2016/1/10)+(2016/5/5- 2016/2/4)+(2016/5/5-2016/3/25)+(2016/4/13-2016/4/13)
for row 2016/7/1, cum_diff_preceding is  0
for row 2016/8/1, cum_diff_preceding is (2016/8/1-2016/7/1)
for row 2016/10/1, cum_diff_preceding is (2016/10/1-2016/7/1)+(2016/10/1- 2016/8/1)
for row 2016/12/1, cum_diff_preceding is (2016/12/1-2016/7/1)+(2016/10/1- 2016/8/1)+(2016/10/1- 2016/10/1)
for row 2016/12/31, cum_diff_preceding is (2016/12/31-2016/7/1)+(2016/10/1- 2016/8/1)+(2016/10/1- 2016/10/1)+(2016/12/31- 2016/12/1)

我使用ddply如下,但它不起作用

>fun_forcast<-function(df){for(i in 2:nrow(df)){df$cum_diff_preceeding[i]<-sum(df$data[i]-df$data[1:(i-1)])}} 
>ddply(df,.(group),transform,cum_diff_preceding<-fun_forcast)

2 个答案:

答案 0 :(得分:0)

这是使用非常简单的for循环的方法:

dat$cum_diff_preceeding <- 0

for(i in 2:nrow(dat)){
  dat$cum_diff_preceeding[i] <- sum(dat$Date[i] - dat$Date[1:(i-1)])
}

        Date cum_diff_preceeding
1 2016-01-10                   0
2 2016-02-04                  25
3 2016-03-25                 125
4 2016-04-13                 182
5 2016-05-05                 270

数据

dat <- structure(list(Date = structure(c(16810, 16835, 16885, 16904, 
16926), class = "Date")), .Names = "Date", row.names = c(NA, 
-5L), class = "data.frame")

        Date
1 2016-01-10
2 2016-02-04
3 2016-03-25
4 2016-04-13
5 2016-05-05

答案 1 :(得分:0)

您可以使用sapply循环播放。

df$data = as.Date(df$data, format = "%Y/%m/%d")  #For converting your values to Date

df$cum_diff_preceding = sapply(1:NROW(df), function(i) sum(df$data[i] - df$data[1:(i-1)]))
df
#        data cum_diff_preceding
#1 2016-01-10                  0
#2 2016-02-04                 25
#3 2016-03-25                125
#4 2016-04-13                182
#5 2016-05-05                270

数据

df = structure(list(data = c("2016/1/10", "2016/2/4", "2016/3/25", 
"2016/4/13", "2016/5/5")), .Names = "data", row.names = c(NA, 
-5L), class = "data.frame")