Suppose I have the following data:
d = data.table( id = 1, x = c(1, 10, 17, 35, 37, 45) )
I want to see if every i-th element in x by group id is from 30 to 40 more than it. So, for the first element in x by group id (1), I am looking to find out if there is any value from x after 1 between values ββ31 and 41. The answer is yes, so I would like to create a valid_gap column that is TRUE for the first item. In the end, I'm looking for:
d_final = data.table( id = 1, x = c(1, 10, 17, 35, 37, 45), valid_gap = c(T, T, F, F, F, F ) )
I thought about this issue with a colleague for a while, and we are really trying to avoid using a loop here, but we cannot understand. Is this possible without a loop?
My best attempt is something like:
d[, valid_gap := any(between( rdist(x[ .N - .I ])[,1], left = 30, right = 40 )), by = id]
but I think of the problem as an attempt to index through x, as if in a loop, which I suspect is the wrong idea.
EDIT - "" :
x = c(1, 10, 17, 35, 37, 45)
valid_gap = c()
for( i in 1:length(x) ) {
if( i == length(x) ){
valid_gap = c(valid_gap, F)
} else {
valid_gap = c(valid_gap, any(between( rdist( x[ x >= x[i] ] )[,1], left = 30, right = 40 )) )
}
}
valid_gap
!