将数字序列分成若干组,一旦达到累积阈值,这些组将重置

编程入门 行业动态 更新时间:2024-10-09 13:33:20
本文介绍了将数字序列分成若干组,一旦达到累积阈值,这些组将重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑此序列,我们可以将其视为事件之间的时间"

Consider this sequence, which we can think of as "time between events"

x <- c(5, 40, 3, 6, 0, 9, 0, 4, 5, 18, 2, 4, 3, 2)

我想将它们分组为30个存储桶,但这些存储桶会重置.期望的结果:

I would like to group these into buckets of 30, but buckets that reset. Desired outcome:

output <- c(0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2)

这是因为,当我们累积到30时,我们将重置"并再次开始计数.因此,5 + 40 > 30,我们下降到零并开始累积加法,直到达到30 ...(3 + 6 + 0 ...),这在我们达到x[10] == 18时发生.

This is because, when we get to a cumulative 30, we "reset" and begin counting again. So, 5 + 40 > 30, we drop down to zero and begin cumulative adding until we reach 30...(3 + 6 + 0 ...), which happens at when we reach x[10] == 18.

推荐答案

一种选择是使用Reduce()计算累计和,当sum超过某个阈值时,可以将sum设置为零:

One option is to use Reduce() to calculate the cumulative sum where you can set the sum to be zero, when it exceeds some threshold:

cumsum(Reduce(function(x, y) if(x < 30) x + y else y, x, acc = T) >= 30) # [1] 0 1 1 1 1 1 1 1 1 2 2 2 2 2

更多推荐

将数字序列分成若干组,一旦达到累积阈值,这些组将重置

本文发布于:2023-10-16 14:11:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497798.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阈值   序列   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!