【问题标题】:How to calculate the cumulative data difference with preceding data如何计算与先前数据的累积数据差异
【发布时间】:2023-04-02 13:12:01
【问题描述】:

我想要的结果就像下面的 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)
……

for 循环是必要的,代码是什么?非常感谢

还有,如果我想按组处理上述内容,我该怎么做?

   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)

【问题讨论】:

    标签: r for-loop cumulative-sum


    【解决方案1】:

    这是一种使用非常简单的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
    

    【讨论】:

    • 非常感谢...它的工作...但是“stucture(list())”是什么意思?你能帮我解释一下吗
    • 如果我的解决方案解决了您的问题,您可以考虑接受它。 structure(list()) 业务是R 存储data.frame 的方式,并且可以使用dput 访问。要亲自查看,请尝试dput(head(mtcars))
    • 嗨@bouncyball,我想继续按组进行上述处理(作为我刚刚更新的考试),我将ddply与您提供给我的代码一起使用,但它不起作用,我花了一整天的时间尝试代码,但它甚至不起作用。你想给我更多建议吗?
    【解决方案2】:

    您可以使用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")
    

    【讨论】:

    • 非常感谢..它运行良好..但我对“stucture(list())”感到困惑..我以前从未见过..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2017-05-15
    相关资源
    最近更新 更多