再次对数据表进行r rowdies迭代 [英] r rowdies iteration on a data table, again
问题描述
至少有几个Q / As与此类似,但我似乎无法掌握它。这是一个可重复的例子。 DT保存数据。我想要食物(n)=食物(n-1)* xRatio.food(n)
There are at least a couple of Q/As that are similar to this but I can't seem to get the hang of it. Here's a reproducible example. DT holds the data. I want food(n) = food(n-1) * xRatio.food(n)
DT <- fread("year c_Crust xRatio.c_Crust
X2005 0.01504110 NA
X2010 NA 0.9883415
X2015 NA 1.0685221
X2020 NA 1.0664189
X2025 NA 1.0348418
X2030 NA 1.0370386
X2035 NA 1.0333771
X2040 NA 1.0165511
X2045 NA 1.0010563
X2050 NA 1.0056368")
最接近公式的代码是
DT[,res := food[1] * cumprod(xRatio.food[-1])]
但res值向上移动,并且第一个值被回收到最后一行并带有警告。我希望xRatio.food的第一个值为NA
but the res value is shifted up, and the first value is recycled to the last row with a warning. I want the first value of xRatio.food to be NA
推荐答案
我要重命名/重塑...
I'd rename/reshape...
myDT = melt(DT, id = "year", meas=list(2,3),
variable.name = "food",
value.name = c("value", "xRatio"))[, food := "c_Crust"][]
# or for this example with only one food...
myDT = DT[, .(year, food = "c_Crust", xRatio = xRatio.c_Crust, value = c_Crust)]
...然后用长形式的数据进行每个食物组的计算:
... then do the computation per food group with the data in long form:
myDT[, v := replace(first(value)*cumprod(replace(xRatio, 1, 1)), 1, NA), by=food]
# or more readably, to me anyways
library(magrittr)
myDT[, v := first(value)*cumprod(xRatio %>% replace(1, 1)) %>% replace(1, NA), by=food]
或者,有 myDT [,v:= c (NA,first(value)* cumprod(xRatio [-1])),by = food]
,扩展了OP的代码,虽然我更喜欢oper使用替换而不是尝试使用 c
来构建向量,因为后者可能遇到奇怪的边缘情况(如果只有一行,就会做对了吗?)。
Alternately, there's myDT[, v := c(NA, first(value)*cumprod(xRatio[-1])), by=food]
, extending the OP's code, though I prefer just operating on full-length vectors with replace rather than trying to build vectors with c
, since the latter can run into weird edge cases (like if there is only one row, will it do the right thing?).
这篇关于再次对数据表进行r rowdies迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!