I am quite new to R and have a question about subset and recombine between two dataframe using range value of one of the variable. So i have my two dataframes like this :
This isn't possible in data.table
nicely. It's FR#203 to implement. You could try package xts
as I think that has this operation.
One long and clunky way (untested) in data.table
is as follows. Say your first table is P
and the 2nd table containing the ranges is R
.
setkey(P,x)
# sort by x and mark as sorted so future queries can use binary search on P
from = P[J(R$min_x),which=TRUE]
# Lookup each min_x in the key of P, returning the location. J stands for Join.
to = P[J(R$max_x),which=TRUE]
# Lookup each max_x in the key of P, returning the location.
len = to-from+1
# vectorized for each item the length to[i]-from[i]+1
i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
# for each item the sequence from[i]:to[i], then concat them all into one vector
cbind(rep(R$id,len), P[i])
# use len to expand the items of R to match what they match to in P