Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Data Science
  3. Question
Data Science

How to combine multiple conditions to subset a data-frame using “OR” ?

Asked by Jack Burgess Jul 15, 2021 1.7K views 2 answers
Share

About this question

 I have a data.frame in R. I want to try two different conditions on two different columns, but I want these conditions to be inclusive. Therefore, I would like to use "OR" to combine the conditions. I have used the following syntax before with a lot of success when I wanted to use the "AND" condition.

my.data.frame <- data[(data$V1 > 2) & (data$V2 < 4>

But I don't know how to use an 'OR' in the above. 

Your answer

2 Answers

Asasenger Latest answer

Answered on Nov 20, 2024

To combine multiple conditions using "OR" in R, you can use the | operator. For example, to subset a data.frame  where either one condition on a column or another condition on a different column is met raft wars, you would use:

my.data.frame <- data[(data$V1 > 2) | (data$V2 < 4>

This selects rows where either V1 > 2 or V2 < 4>

Was this helpful?

More Data Science discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Data Science Blogs

Guides, tips and career advice on Data Science from JanBask experts.