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

StackOverflow https://stackoverflow.com/questions/2609647

  •  25-09-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top