- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to convert values greater than a threshold into 1 in column of a matrix in R?
- How to convert values greater than a threshold into 1 in column of a data.table object in R?
- How to convert a data frame into two column data frame with values and column name as variable in R?
- How to select values less than or greater than a specific percentile from an R data frame column?
- How to convert first letter into capital in R data frame column?
- How to convert multiple columns into single column in an R data frame?
- How to convert values in alternate rows to negative in R data frame column?
- How to convert a data frame column to date that contains integer values in R?
- Find the frequency of successive occurrences less than equal to a threshold in an R data frame column?
- How to convert a vector into data frame in R?
- How to check if a variable contains number greater than 1 in an R data frame?
- How to save column means into a data frame in R?
- How to convert the character values in an R data frame column to lower case?
- How to convert first letter into capital in single column R data frame using a function?
- How to repeat column values in R data frame by values in another column?

Advertisements