累积和直到达到最大值,然后在下一行从零开始重复 [英] Cumulative sum until maximum reached, then repeat from zero in the next row

查看:22
本文介绍了累积和直到达到最大值,然后在下一行从零开始重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个相当简单的问题,但对于我的生活,我似乎无法找到答案.我有一个相当标准的数据框,我想要做的是对一列值求和,直到它们达到某个值(确切值或大于它),此时它将 1 放入一个新列(标记为保持)并在 0 处重新开始求和.

I feel like this is a fairly easy question, but for the life of me I can't seem to find the answer. I have a fairly standard dataframe, and what I am trying to do is sum the a column of values until they reach some value (either that exact value or greater than it), at which point it drops a 1 into a new column (labelled keep) and restarts the summing at 0.

我有一列分钟、分钟之间的差异、一个保持列和一个累积总和列(我使用的示例比实际的完整数据集清晰得多)

I have a column of minutes, the differences between the minutes, a keep column, and a cumulative sum column (the example I am using is much cleaner than the actual full dataset)

 minutes     difference     keep     difference_sum
 1052991158       0          0            0
 1052991338      180         0            180
 1052991518      180         0            360
 1052991698      180         0            540
 1052991878      180         0            720
 1052992058      180         0            900
 1052992238      180         0            1080
 1052992418      180         0            1260
 1052992598      180         0            1440
 1052992778      180         0            1620
 1052992958      180         0            1800

差和列是用代码计算的

caribou.sub$difference_sum<-cumsum(difference)

我想做的是运行上面的代码,条件是,当求和值达到 1470 或任何大于它的数字时,它在保持列中放置一个 1,然后重新开始求和,并在整个过程中继续运行数据集.

What I would like to do is run the above code with the condition that, when the summed value reaches either 1470 or any number greater than that it puts a 1 in the keep column and then restarts summing afterwards, and continues running throughout the dataset.

提前致谢,如果您需要更多信息,请告诉我.

Thanks in advance, and if you need any more information let me know.

艾登

推荐答案

我认为这最好用 for 循环来完成,想不出一个可以开箱即用的函数.以下应该做你想做的(如果我理解正确的话).

I think this is best done with a for loop, can't think of a function that could do so out of the box. The following should do what you want (if I understand you correctly).

current.sum <- 0
for (c in 1:nrow(caribou.sub)) {
    current.sum <- current.sum + caribou.sub[c, "difference"]
    carribou.sub[c, "difference_sum"] <- current.sum
    if (current.sum >= 1470) {
        caribou.sub[c, "keep"] <- 1
        current.sum <- 0
    }
}

如果它不完全是您想要的,请随时发表评论.但是正如alexwhan所指出的,您的描述并不完全清楚.

Feel free to comment if it does not exactly what you want. But as pointed out by alexwhan, your description is not completely clear.

这篇关于累积和直到达到最大值,然后在下一行从零开始重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆