Juan Pablo
Juan Pablo

Reputation: 13

What is an efficient way to do this in R?

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

Answers (2)

akrun
akrun

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

jay.sf
jay.sf

Reputation: 72758

It might be worth to give subset a try.

subset(x, a %in% b | a %in% y[y$c, 'd'])

Upvotes: 1

Related Questions