I want to get a list of values that fall between several ranges.
library(data.table) values <- data.table(value = c(1:100)) range <- data.table(start = c(6, 29, 87), end = c(10, 35, 92))
I need results to include only the values that fall between these ranges:
results <- c(6, 7, 8, 9, 10, 29, 30, 31, 32, 33, 34, 35, 87, 88, 89, 90, 91, 92)
I am currently doing this with a for loop,
results <- data.table(NULL) for (i in 1:NROW(range){ results <- rbind(results, data.table(result = values[value >= range[i, start] & value <= range[i, end], value]))}
however, the actual dataset is quite large, and I'm looking for a more efficient way.
Any suggestions are welcome! Thanks!
r range data.table subset
son.ra
source share