如何使用data.table选择一行及其下一行和上一行

时间:2019-04-11 11:15:57

标签: r data.table

我有此数据表:

> dput(data_DT)

    structure(
     list(Date = structure(
                 c(1512518400, 1512518400, 1512518400,
                   1512518400, 1512518400, 1512518400), 
                 class = c("POSIXct", "POSIXt"), 
                 tzone = "UTC"), 
          Time = structure(
                 c(1512573600, 1512573300, 1512573000, 
                   1512572700, 1512572400, 1512572100), 
                 class = c("POSIXct", "POSIXt"), 
                 tzone = "UTC"), 
          High = c(46, 47, 49, 49, 49, 58),
          High_lag1 = c(47, 49, 49, 49, 58, 60), 
          Low = c(45, 46, 46, 47, 43, 44), 
          Low_lag1 = c(46, 46, 47, 43, 44, 58), 
          tr = c(1, 3, 2, 6, 14), 
          tr_lag1 = c(1, 3, 2, 6, 14, 2)
        ), 
     row.names = c(NA, -6L), 
     class = c("data.table", "data.frame"))

我想根据某些条件选择一些行,然后选择上一行或下一行

这是我到目前为止的代码

data_DT1 <- data_DT[Low < Low_lag1 & High < High_lag1 & tr > 13]

代码选择我想要的时间为14:55的行,但在这种情况下,我还需要时间为15:00的行。

此外,在某些情况下,我将选择一行,并且需要上一行。

1 个答案:

答案 0 :(得分:1)

您可以使用which()函数来返回条件的索引,然后根据需要添加或减去:

# get row and next row :
data_DT[which(Low < Low_lag1 & High < High_lag1 & tr > 13)+c(0,1)]
#          Date                Time High High_lag1 Low Low_lag1 tr tr_lag1
# 1: 2017-12-06 2017-12-06 14:55:00   58        60  44       58 14       2
# 2:       <NA>                <NA>   NA        NA  NA       NA NA      NA

# get previous row and row
data_DT[which(Low < Low_lag1 & High < High_lag1 & tr > 13)+c(-1,0)]
#          Date                Time High High_lag1 Low Low_lag1 tr tr_lag1
# 1: 2017-12-06 2017-12-06 15:00:00   49        58  43       44  6      14
# 2: 2017-12-06 2017-12-06 14:55:00   58        60  44       58 14       2