假设我有一个数据集 dt
dt <- as.data.table(mtcars)
对于 'cyl' 变量的第一行,我想将新列 test 更新为 'qsec' 变量的值。另外,我不想放弃其他观察结果,而是将它们的值 pf test 为零。
下面的代码给出了第一行。但我对如何更新列并保留所有内容感到困惑
dt[order(cyl), .SD[c(1)], by=cyl]
所需输出的示例
mpg cyl disp hp drat wt qsec vs am gear carb test
1: 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 18.61
2: 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 16.46
3: 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 0.00
4: 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 0.00
5: 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 0.00
6: 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 17.02
注意:它是针对大数据的,因此如果有一个运行速度更快的高效代码,我将不胜感激。
最佳答案
我们可以使用 rowid
来完成此操作,无需分组
library(data.table)
dt[, test := (rowid(cyl) == 1) * qsec]
-输出
head(dt, 8)
# mpg cyl disp hp drat wt qsec vs am gear carb test
#1: 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 16.46
#2: 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 0.00
#3: 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 18.61
#4: 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 0.00
#5: 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 17.02
#6: 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 0.00
#7: 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 0.00
#8: 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 0.00
或者另一个选项是.I
,它非常快
dt[, test := 0] # // create a column of 0's
i1 <- dt[, .I[1], cyl]$V1 # // get the index of the first element for each cyl
dt[i1, test := qsec] # // specify it in i and update the test
关于r - 如果首先然后在 R 中的新列中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65091338/