R: How to pass a list of selection expressions (strings in this case) to the subset function?
Her开发者_如何学编程e 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?
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)
精彩评论