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

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

问题描述

我觉得这是一个相当容易的问题,但对于我的生活,我似乎无法找到答案。我有一个相当标准的数据框,我想要做的是将一列值相加,直到它们达到某个值(精确值或大于它),此时它将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)使用比实际的完整数据集更干净)

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.

Ayden

推荐答案

我认为这最好用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天全站免登陆