再次在数据表上进行 r rowdies 迭代 [英] r rowdies iteration on a data table, again

查看:30
本文介绍了再次在数据表上进行 r rowdies 迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至少有几个与此类似的 Q/A,但我似乎无法掌握它.这是一个可重现的示例.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 的代码,虽然我更喜欢用 replace 对全长向量进行操作,而不是尝试用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆