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
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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) 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top