How to check if a variable contains number greater than 1 in an R data frame?

The variables in an R data frame are referred to as the columns of the data frame. Sometimes we have a threshold value for a particular column and we need to check whether all the values in that column are greater than or less than the threshold. For this purpose, we can make use of ifelse function as shown in the below examples.

Example1

y<−rpois(20,1)
df2<−data.frame(y)
df2

Output

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

Checking whether values in column y are greater than 1 or not −

Example

ifelse(df2$y>1,"Yes","No")

Output

[1] "No" "No" "No" "No" "No" "No" "Yes" "Yes" "Yes" "No" "No" "No"
[13] "No" "Yes" "No" "No" "No" "Yes" "No" "No"

Example3

z<−sample(0:5,20,replace=TRUE)
df3<−data.frame(z)
df3

Output

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

Checking whether values in column z are greater than 1 or not −

Example

ifelse(df3$z>1,"Yes","No")

Output

[1] "Yes" "Yes" "No" "No" "No" "No" "Yes" "Yes" "Yes" "Yes" "No" "No"
[13] "No" "No" "Yes" "Yes" "Yes" "No" "No" "Yes"
Updated on: 2026-03-11T22:50:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements