How to check whether a column value is less than or greater than a certain value in R?


To check whether a column value is less than or greater than a certain value, we can use with function and the output will be a logical vector representing values with TRUE when the condition is satisfied and FALSE when the condition is not satisfied. For example, if we have a column say x of an R data frame df and we want to check whether any of the values in x is greater than 10 or not then it can be done by using with(df,df$x>10).

Example

Consider the below data frame:

Live Demo

> set.seed(1002)
> x1<-rpois(20,5)
> y1<-rpois(20,8)
> z1<-rpois(20,3)
> df1<-data.frame(x1,y1,z1)
> df1

Output

x1 y1 z1
1 5 6 1
2 7 8 2
3 5 9 2
4 3 4 2
5 4 10 3
6 6 6 1
7 10 8 6
8 6 3 6
9 4 12 1
10 8 13 2
11 6 7 4
12 8 9 3
13 5 8 4
14 5 4 3
15 2 7 5
16 4 7 4
17 6 14 3
18 7 6 2
19 8 7 1
20 5 9 5

Checking the conditions for different values.

Example

> with(df1,df1$x1<5)
[1] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[13] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
> with(df1,df1$x1>5)
[1] FALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
[13] FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
> with(df1,df1$y1>6)
[1] FALSE TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE TRUE
[13] TRUE FALSE TRUE TRUE TRUE FALSE TRUE TRUE
> with(df1,df1$y1>8)
[1] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
[13] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
> with(df1,df1$z1>8)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> with(df1,df1$z1<4)
[1] TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
[13] FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE
> with(df1,df1$x1>7)
[1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE
[13] FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE

Let’s have a look at another example.

Example

Live Demo

> x2<-sample(0:9,20,replace=TRUE)
> y2<-sample(0:9,20,replace=TRUE)
> z2<-sample(0:9,20,replace=TRUE)
> df2<-data.frame(x2,y2,z2)
> df2

Output

x2 y2 z2
1 1 3 4
2 5 9 7
3 5 2 9
4 8 8 7
5 2 8 8
6 1 3 6
7 3 5 6
8 5 5 2
9 6 0 4
10 1 6 9
11 9 6 3
12 0 3 7
13 2 4 3
14 1 6 8
15 5 1 4
16 2 0 7
17 6 7 8
18 9 5 9
19 1 3 8
20 5 4 0

Example

> with(df2,df2$x2>6)
[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
[13] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
> with(df2,df2$x2>5)
[1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE FALSE
[13] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
> with(df2,df2$y2>5)
[1] FALSE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE FALSE
[13] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
> with(df2,df2$y2>3)
[1] FALSE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
[13] TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
> with(df2,df2$y2<3)
[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
[13] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
> with(df2,df2$x2<5)
[1] TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE TRUE
[13] TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE

Updated on: 07-Nov-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements