df <- data.frame(loc.id = rep(1:10, each = 10),
MG = rep(1:10,times = 10),
x = runif(100))
If I want to filter the data based on multiple conditions, I could do this:
df %>% filter(MG > 5 & loc.id < 4)
However, I have a situtation where filtering conditions are different. For e.g
If loc.id
is less than 4, only keep the MG
1-4
If loc.id
is between 5 to 6, only keep the MG
5-8
If loc.id
is greater than 6, only keep the MG greater than 8.
mDT = rbindlist(list(CJ(loc.id = 1:3, MG = 1:4), CJ(5:6, 5:8), CJ(7:10, 9:10))); setDT(df)[mDT, on=names(mDT), nomatch=0]
or similarly with dplyr. – Frank