views:

79

answers:

1

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?

+1  A: 

You probably want the %in% operator

> dat <- data.frame(series = c("1a", "1b", "1e"), reading = c(0.1, 0.4, 0.6))
> series_you_want <- c("1a", "1e")
> subset(dat, series %in% series_you_want) 
rcs
Thanks, RCS, that works great, I also added this to make listing the series easier.series_you_want = "1a,1b,1e"series_you_want_list = unlist(strsplit(series_you_want, ","))
John