简体   繁体   English

R data.table当前和所有先前行的最大值

[英]R data.table max of current and all previous rows

I have a data.table named "sampleDT" with column c1 and c2 我有一个名为“ sampleDT”的data.table,具有列c1和c2

require(data.table)
sampleDT <- data.table(c1 = c(1,2,3), c2 = c(7,5,6), result = c(NA, 7, 7))
print(sampleDT)
> print(sampleDT)enter code here
   c1 c2 result
1:  1  7     NA
2:  2  5      7
3:  3  6      7

I would like to create a new column 'result'; 我想创建一个新的列“结果”; and the value is to take the maximum from previous row: 值应取上一行的最大值:

  • 1st row of column 'result' would inevitably be NA; 列“结果”的第一行将不可避免地为NA;
  • 2nd row would give 7; 第二行将给出7;
  • 3rd row would also give 7, because sampleDT[2,result] == 7 . 第三行也会给出7,因为sampleDT[2,result] == 7

We can obviously use for-loop to achieve it; 我们显然可以使用for循环来实现它; I'm wondering if there's any faster solution? 我想知道是否有更快的解决方案? (Note: not sure if shift would work, because result is dependent on the previous row. (注意:不确定shift是否可以工作,因为结果取决于上一行。

You can leverage data.table 's chaining: 您可以利用data.table的链接:

# load package
require(data.table)

# create dummy data
sampleDT <- data.table(c1 = c(1,2,3), c2 = c(7,5,6))

# run code
sampleDT[, result := shift(apply(.SD, 1, max)), 
           .SDcols = 1:2][, result := shift(apply(.SD, 1, max, na.rm = TRUE)), 
                            .SDcols = 1:3][]

>
   c1 c2 result
1:  1  7     NA
2:  2  5      7
3:  3  6      7

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM