首页 > 解决方案 > 按前一行和实际行定义值

问题描述

我有data.table两个字段,startvalueendValue,我需要根据前一行和实际行中的一些信息进行填充。虽然这在某种程度上类似于thisthis,但我无法得到我想要的结果。

虚拟数据:

a <- data.table(user = c("A", "A", "A", "B", "B"), 
                gap = c(1, 0, 2, 2, 3), 
                priority = c(1, 3, 2, 2, 1))

然后我修复startValue所有优先级== 1:

setkey(a, user, priority)
a[priority == 1, startValue := 0]

endValue为那些startValue已经定义的设置:

a[!is.na(startValue), endValue := startValue + gap*3]

现在问题来了。我希望第startValue2 行(用户 A,优先级 2)与第 1 行相同endValue,所以我可以计算新的endValue. 我知道我可以使用循环,但我想知道是否可以通过使用任何其他函数或函数组合来做到这一点。

我尝试了几种组合,shiftzoo:na.locf总是弄乱了已经存在的值。

预期结果:

b <- structure(list(user = c("A", "A", "A", "B", "B"), 
                    gap = c(1, 2, 0, 3, 2), 
                    priority = c(1, 2, 3, 1, 2), 
                    startValue = c(0, 3, 9, 0, 9), 
                    endValue = c(3, 9, 9, 9, 15)), 
               row.names = c(NA, -5L), 
               class = c("data.table", "data.frame"))

标签: rdata.tablelocf

解决方案


我们可以accumulate使用purrr

library(purrr)
library(data.table)
a[, endValue := accumulate(gap,  ~   .x + .y * 3, .init = 0)[-1], user
   ][, startValue := shift(endValue, fill = 0), user][]
all.equal(a, b, check.attributes = FALSE)
#[1] TRUE

或者使用Reducefrombase R创建 'endValue' 列,然后使用lag'endValue' 来创建按 'user' 分组的 'startValue'

a[, endValue := Reduce(function(x, y) x + y *3, gap, 
     accumulate = TRUE, init = 0)[-1], user]

推荐阅读