【发布时间】:2023-03-15 19:16:02
【问题描述】:
我有如下数据表:
library(data.table)
dt <- data.table("one" =c(100,200,300,400,500,600))
dt <- dt[,two:=round(one*1.05,0)][,three:=round(two*1.03,0)][,four:=round(three*1.07,0)][,five:=round(four*1.05,0)][,six:=round(five*1.1,0)][,curr:=c(3,4,5,6)]
> dt
one two three four five six curr
1: 100 105 108 116 122 134 3
2: 200 210 216 231 243 267 4
3: 300 315 324 347 364 400 5
4: 400 420 433 463 486 535 6
5: 500 525 541 579 608 669 3
6: 600 630 649 694 729 802 4
我想使用 data.table 逐行抓取“curr”列中的数字,并计算表中该列号及其前一列的平均值。例如,对于第一行,它将在“第三”列中获取 108 的值,在“第二”列中获取 105 的值并给出 106.5。对于第二行,它将取 231 和 216 等的平均值。
【问题讨论】:
标签: r data.table