Reputation: 13
In R, I am trying to filter out all cases from x whose $a is in y$b or y$d (when y$c is true).
x[(x$a %in% y$b | x$a %in% y[y$c == TRUE, ]$d), ]
Is this right? Is there a better way to do this? Thanks
Upvotes: 1
Views: 60
Reputation: 887088
If it is a logical column, no need to == TRUE
. Also, when subsetting a single column, directly subset instead of subsetting it from the data.frame which is inefficient
x[(x$a %in% y$b | x$a %in% y$d[y$c]), ]
Or make it a bit more compact
x[(x$a %in% c(y$b, y$d[y$c])),]
Upvotes: 1
Reputation: 72758
It might be worth to give subset
a try.
subset(x, a %in% b | a %in% y[y$c, 'd'])
Upvotes: 1