# How to convert values greater than a threshold into 1 in R data frame column?

To convert values greater than a threshold into 1 in R data frame column, we can follow the below steps −

• First of all, create a data frame.

• Then, use ifelse function to convert values greater than a threshold into 1.

## Example

#### Create the data frame

Let’s create a data frame as shown below −

x<-sample(1:10,25,replace=TRUE)
df<-data.frame(x)
df

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    x
1   1
2   7
3   5
4   1
5   6
6   1
7   1
8   9
9   2
10  5
11  8
12 10
13  7
14  2
15  6
16  7
17  1
18  6
19  9
20  9
21  3
22  9
23 10
24  1
25  7

Convert values greater than a threshold to 1

Using ifelse function to convert values greater than a threshold into 1 in column x of data frame −

x<-sample(1:10,25,replace=TRUE)
df<-data.frame(x)
df$x<-ifelse(df$x>5,1,df\$x)
df

## Output

   x
1  1
2  1
3  5
4  1
5  1
6  1
7  1
8  1
9  2
10 5
11 1
12 1
13 1
14 2
15 1
16 1
17 1
18 1
19 1
20 1
21 3
22 1
23 1
24 1
25 1