I have a data.table and I need to extract equal length segments starting at various row locations. What is the easiest way to do this? For example:
x <- data.table(a=sample(1:1000,100), b=sample(1:1000,100))
r <- c(1,2,10,20,44)
idx <- lapply(r, function(i) {j <-which(x$a == i); if (length(j)>0) {return(j)} })
y <- lapply(idx, function(i) {if (!is.null(i)) x[i:(i+5)]})
do.call(rbind, y)
a b
1: 44 63
2: 96 730
3: 901 617
4: 446 370
5: 195 341
6: 298 411
This is certainly not the data.table way of doing things so I was hoping there is a better way?
EDIT: Per comments below, I edit this just so it’s clear that the values in a are not necessarily contiguous nor do they correspond to the row number.
Not sure whether you already know the row positions, or if you want to search for them. Either way, this should cover both.
Option 1: make the row number sequences, concatenate, and do one lookup in
DT(no need for keys or binary search just to select by row numbers) :Option 2: make a list of data.table subsets and then
rbindthem together. This is less efficient than option 1, but for completeness :