Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

r - How to subset by additional condition in same column

I'm going through some subsetting examples with mtcars, like here:

mtcars[mtcars$gear == 5, ]

now this returns me all the rows in mtcars where gear = 5, but how do I tell it for example to also return the rows where gear = 4 as well?

I have been looking at logical operators and such, but neither & nor | seems to work.

question from:https://stackoverflow.com/questions/65891692/how-to-subset-by-additional-condition-in-same-column

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can use %in% instead of ==

mtcars[mtcars$gear %in%  c(4, 5), ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...