When selecting rows with a numeric index, is there a good way to only select rows that exist? For example, I'd like:
library(data.table)
dt <- data.table(x = 1:2)
dt[1:4, ]
#> x
#> 1: 1
#> 2: 2
#> 3: NA
#> 4: NA
I need this to match the semantics of dplyr::slice() (tidyverse/dtplyr#10).
I also need to do this by-group, so I came up with this:
library(data.table)
dt <- data.table(x = 1:4, y = c(1, 2, 1, 2))
dt[dt[, na.omit(.I[1:3]), by = "y"]$V1]
#> x y
#> 1: 1 1
#> 2: 3 1
#> 3: 2 2
#> 4: 4 2
The problem with this approach is that it refers to dt twice; this is problematic if dt is actually a compound expression because it will require dt to be evaluated twice. I could assign it to a variable, but I was hoping to find some way to do it in a single sequence of call.
When selecting rows with a numeric index, is there a good way to only select rows that exist? For example, I'd like:
I need this to match the semantics of
dplyr::slice()(tidyverse/dtplyr#10).I also need to do this by-group, so I came up with this:
The problem with this approach is that it refers to
dttwice; this is problematic ifdtis actually a compound expression because it will requiredtto be evaluated twice. I could assign it to a variable, but I was hoping to find some way to do it in a single sequence of call.