Check to see if a value is within a range?

Here's a dplyr option in case anybody stumbles on this question:

library(dplyr)
value = 1
df %>% 
  mutate(ok = value >= time.s & value <= time.e)

  ID time.s time.e    ok
1  1      1      2  TRUE
2  2      1      4  TRUE
3  3      2      3 FALSE
4  4      2      4 FALSE

Assuming that the values of ID are unique:

DT[, list(OK = 1 %in% seq(time.s, time.e)), by = ID]

giving;

   ID    OK
1:  1  TRUE
2:  2  TRUE
3:  3 FALSE
4:  4 FALSE

Also, this works:

with(dat, time.s <= 1 & time.e >= 1)

Tags:

R