Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

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.

1 Answer

0 votes
by
edited by

To combine multiple conditions to subset a data frame using ‘OR’, use the following:

my.data.frame <- subset(data , V1 > 2 | V2 < 4)

You can also use the filter function from the dplyr package as follows:

library(dplyr) 

filter(data, V1 > 2 | V2 < 4)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...