0
votes
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.

2
One option would be enumerating combos and left-joining: with data.table 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

2 Answers

4
votes

Why not this:

df %>% filter( (loc.id<4 & between(MG, 1,4)) | (between(loc.id, 5, 6) & between(MG, 5, 8)) | (loc.id>6 & MG>8))

0
votes

When i need to filter a dataframe i prefer to use the function which ex:

dfalt <- df[which(df$MG > 5 & df$loc.id < 4), ]

That works good for me!