- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想计算cumsum
从每次运行信号开始的某个值,其中 signal == 1
.
示例数据:
set.seed(123)
df <- data.frame(Date = seq.Date(as.Date('2016-09-01'),as.Date('2016-09-30'),by = 'days'),
value = sample(1:10,size=30,replace = TRUE),
signal = c(rep(0,3),rep(1,2),rep(0,1),rep(1,5),rep(0,6),rep(1,3),rep(0,5),rep(1,5)))
> head(df,12)
Date value signal
1 2016-09-01 10 0
2 2016-09-02 10 0
3 2016-09-03 7 0
4 2016-09-04 8 1
5 2016-09-05 1 1
6 2016-09-06 5 0
7 2016-09-07 8 1
8 2016-09-08 3 1
9 2016-09-09 4 1
10 2016-09-10 3 1
11 2016-09-11 2 1
12 2016-09-12 5 0
dplyr
有一种更有效、更优雅的方法来做到这一点。或
data.table
.
df$pl <- rep(0,length(df))
# calculating the indices of start/end of runs where signal == 1
runs <- rle(df$signal)
start <- cumsum(runs$lengths) +1
start <- start[seq(1, length(start), 2)]
end <- cumsum(runs$lengths)[-1]
end <- end[seq(1, length(end), 2)]
for(i in 1:length(start))
{
df$pl[start[i]:end[i]] <- cumsum(df$value[start[i]:end[i]])
}
> head(df,12)
Date value signal pl
1 2016-09-01 10 0 0
2 2016-09-02 10 0 0
3 2016-09-03 7 0 0
4 2016-09-04 8 1 8
5 2016-09-05 1 1 9
6 2016-09-06 5 0 0
7 2016-09-07 8 1 8
8 2016-09-08 3 1 11
9 2016-09-09 4 1 15
10 2016-09-10 3 1 18
11 2016-09-11 2 1 20
12 2016-09-12 5 0 0
最佳答案
使用 data.table
,你可以这样做
library(data.table)
set.seed(123)
seq.Date(as.Date('2016-09-01'),as.Date('2016-09-30'),by = 'days')
sample(1:10,size=30,replace = TRUE)
c(rep(0,3),rep(1,2),rep(0,1),rep(1,5),rep(0,6),rep(1,3),rep(0,5),rep(1,5))
df <- data.table(Date = seq.Date(as.Date('2016-09-01'),as.Date('2016-09-30'),by = 'days'),
value = sample(1:10,size=30,replace = TRUE),
signal = c(rep(0,3),rep(1,2),rep(0,1),rep(1,5),rep(0,6),rep(1,3),rep(0,5),rep(1,5)))
df[, pl := cumsum(value)*signal, by = .(signal, rleid(signal))]
#> Date value signal pl
#> 1: 2016-09-01 10 0 0
#> 2: 2016-09-02 10 0 0
#> 3: 2016-09-03 7 0 0
#> 4: 2016-09-04 8 1 8
#> 5: 2016-09-05 1 1 9
#> 6: 2016-09-06 5 0 0
#> 7: 2016-09-07 8 1 8
#> 8: 2016-09-08 3 1 11
#> 9: 2016-09-09 4 1 15
#> 10: 2016-09-10 3 1 18
#> 11: 2016-09-11 2 1 20
#> 12: 2016-09-12 5 0 0
#> 13: 2016-09-13 5 0 0
#> 14: 2016-09-14 4 0 0
#> 15: 2016-09-15 2 0 0
#> 16: 2016-09-16 2 0 0
#> 17: 2016-09-17 3 0 0
#> 18: 2016-09-18 5 1 5
#> 19: 2016-09-19 3 1 8
#> 20: 2016-09-20 9 1 17
#> 21: 2016-09-21 1 0 0
#> 22: 2016-09-22 5 0 0
#> 23: 2016-09-23 8 0 0
#> 24: 2016-09-24 2 0 0
#> 25: 2016-09-25 6 0 0
#> 26: 2016-09-26 3 1 3
#> 27: 2016-09-27 2 1 5
#> 28: 2016-09-28 8 1 13
#> 29: 2016-09-29 9 1 22
#> 30: 2016-09-30 4 1 26
#> Date value signal pl
dplyr
,我不知道任何等效的
data.table::rleid
,所以它使用它:
library(dplyr)
df %>%
group_by(id = data.table::rleidv(signal)) %>%
mutate(pl = cumsum(value) * signal) %>%
select(-id) %>%
head(12)
#> Adding missing grouping variables: `id`
#> Source: local data frame [12 x 5]
#> Groups: id [5]
#>
#> id Date value signal pl
#> <int> <date> <int> <dbl> <dbl>
#> 1 1 2016-09-01 10 0 0
#> 2 1 2016-09-02 10 0 0
#> 3 1 2016-09-03 7 0 0
#> 4 2 2016-09-04 8 1 8
#> 5 2 2016-09-05 1 1 9
#> 6 3 2016-09-06 5 0 0
#> 7 4 2016-09-07 8 1 8
#> 8 4 2016-09-08 3 1 11
#> 9 4 2016-09-09 4 1 15
#> 10 4 2016-09-10 3 1 18
#> 11 4 2016-09-11 2 1 20
#> 12 5 2016-09-12 5 0 0
关于r - 通过运行信号值计算 `cumsum`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40283371/
我正在尝试在 pandas 中找到一个矢量化解决方案,该解决方案在电子表格中很常见,即在基于实际 cumsum 的结果跳过或设置固定值的情况下进行 cumsum。我有以下内容: A 1 0
假设我有一个距离数组 x=[1,2,1,3,3,2,1,5,1,1]。 我想从 x 中获取索引,其中 cumsum 达到 10,在本例中,idx=[4,9]。 所以 cumsum 在满足条件后重新启动
每当超过给定的 cumsum 阈值时,我需要有关对连续行进行分组的帮助。当超过此阈值时,也应重新启动 cumsum(为零),如下所示: Index Values Regular CumS
我对 python 还很陌生。我尝试对每个客户进行累积总和,以查看相应的不活动月份(标志:1 或 0)。因此,当我们有 0 时,需要重置 1 的累积和。当我们有新客户端时,也需要重置。请参阅下面的示例
我要重置 cumsum在一个向量上,当它达到某个值时。 例如。对于以下向量: v <- c(3, 5, 2, 5, 3, 4, 5, 3, 1, 4) 预期输出为: c(0, 0, 10, 0, 0,
我正在尝试使用reduce函数在python中编写累积和的版本。这是到目前为止我的代码: from functools import reduce def my_cum_sum(arg): r
问题:给定一个二叉搜索树,其中的键是数字,我们将定义操作“cumsum”(累积和的简写),它切换树中每个节点的键所有小于或等于它的键的总和。 例如, 在这个例子中, 根中的键 5 切换为值 10:根中
我是 pandas 的新手,我可以将 cumsum 添加为 df.cumsum(axis=1) y0 y1 y2 0 2 3 4 1 2 2 3 2 0 0
我有一个正数向量,使得 - 如果数字大于或等于 1,则相加 - 如果数字小于 1,则乘以累积和。 例如 > set.seed(0) > x x [1] 3.0 0.9 0.9 1.0 3.0 0.
我想计算cumsum从每次运行信号开始的某个值,其中 signal == 1 . 示例数据: set.seed(123) df head(df,12) Date value sig
这个问题在这里已经有了答案: Cumsum with reset when 0 is encountered and by groups (2 个回答) Cumulative sum that res
dict={"asset":["S3","S2","E4","E1","A6","A8"], "Rank":[1,2,3,4,5,6],"number_of_attributes":[2,1,2,2,
这个问题在这里已经有了答案: Restart cumsum and get index if cumsum more than value (3 个答案) 关闭 2 年前。 我想在每次超过阈值 10
我想修改 cumsum 函数。我想将负值更改为 0。并且当有一个不同于 0 的新 D 值时,则使用 D-S。 下面的例子: TD % group_by(product) %>% mutate(res
是否可以在一列中迭代使用 cumsum() 并在另一列上以开始 - 停止为条件: 给定数据帧 df 和一列 X 其中值是递增的。 cumsum() 应在达到 10 或 10 的倍数时停止(例如 20、
带有数据框: df <- data.frame(id = rep(1:3, each = 5) , hour = rep(1:5, 3)
In [46]: d = np.random.randn(10, 1) * 2 In [47]: df = pd.DataFrame(d.astype(int), columns=['data'])
我目前正在尝试创建一个累积总和列,该列将根据 Game_ID 创建一个累积总和,但一次只计算与 Game_ID 相关的值。例如,玩家 A 在 Game_ID == 1 中拍摄 20 次,在 Game_
我正在尝试计算每组从最后一行到第一行的cumsum。 示例数据: t1 ts var val val2 1 a 0 2 2 a 0 2 3 a 0
我有一个矩阵,例如: A= [ 1 2 3 4 3 5 6 6 4 1 2 3 ] 我想以另一个矩阵(或数据框)的形式获取列的累积和。例如,这个矩阵会给出: B= [1 2 3 4
我是一名优秀的程序员,十分优秀!