R: How to pass a list of selection expressions (strings in this case) to the subset function?

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-04-09T17:51:31Z Indexed on 2010/04/09 17:53 UTC
Read the original article Hit count: 267

Filed under:
|
|

Here is some example data:

data = data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6))

> data
  series reading
1     1a     0.1
2     1b     0.4
3     1e     0.6

Which I can pull out selective single rows using subset:

> subset (data, series == "1a")
  series reading
1     1a     0.1

And pull out multiple rows using a logical OR

> subset (data, series == "1a" | series  == "1e")
  series reading
1     1a     0.1
3     1e     0.6

But if I have a long list of series expressions, this gets really annoying to input, so I'd prefer to define them in a better way, something like this:

series_you_want = c("1a", "1e")  (although even this sucks a little)

and be able to do something like this,

subset (data, series == series_you_want)

The above obviously fails, I'm just not sure what the best way to do this is?

© Stack Overflow or respective owner

Related posts about r

    Related posts about subset