niharika Singhal niharika Singhal - 3 months ago 10
R Question

How to write OR condition inside which in R

I am unable to figure out how can i write or condition inside which in R.
This statemnet does not work.

which(value>100 | value<=200)


I know it very basic thing but i am unable to find the right solution.

Thanks

Answer

Every value is either larger than 100 or smaller-or-equal to 200. Maybe you need other numbers or & instead of |? Otherwise, there is no problem with that statement, the syntax is correct:

> value <- c(110, 2, 3, 4, 120)
> which(value>100 | value<=200)
[1] 1 2 3 4 5
> which(value>100 | value<=2)
[1] 1 2 5
> which(value>100 & value<=200)
[1] 1 5