- 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
Convert a numeric column to binary factor based on a condition in R data frame
To convert a numeric column to binary factor based on a condition in R data frame, we can use factor function along with ifelse function.
For Example, if we have a data frame called df that contains a numerical column say Num and we want to convert it to a binary factor if Num is less than 100 then it will be Minor otherwise Major then we can use the below given command −
df$Num_Factor<-factor(ifelse(df$Num_Factor<100,"Minor","Major"))
Example 1
Following snippet creates a sample data frame −
x<-sample(1:50,20) df1<-data.frame(x) df1
The following dataframe is created
x 1 4 2 13 3 50 4 19 5 43 6 42 7 18 8 17 9 27 10 23 11 31 12 37 13 5 14 28 15 1 16 6 17 30 18 35 19 22 20 10
To convert x into a factor column on the above created data frame, add the following code to the above snippet −
x<-sample(1:50,20) df1<-data.frame(x) df1$x_Factor<-factor(ifelse(df1$x<25,"Low","High")) df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x x_Factor 1 4 Low 2 13 Low 3 50 High 4 19 Low 5 43 High 6 42 High 7 18 Low 8 17 Low 9 27 High 10 23 Low 11 31 High 12 37 High 13 5 Low 14 28 High 15 1 Low 16 6 Low 17 30 High 18 35 High 19 22 Low 20 10 Low
Example 2
Following snippet creates a sample data frame −
y<-round(rnorm(20,1,0.25),1) df2<-data.frame(y) df2
The following dataframe is created
y 1 1.4 2 0.9 3 1.0 4 0.6 5 1.1 6 0.7 7 0.9 8 1.2 9 0.8 10 1.1 11 0.7 12 0.4 13 1.1 14 0.8 15 1.1 16 0.7 17 1.7 18 0.8 19 1.1 20 0.6
To convert y into a factor column on the above created data frame, add the following code to the above snippet −
y<-round(rnorm(20,1,0.25),1) df2<-data.frame(y) df2$y_Factor<-factor(ifelse(df2$y<1,"Rejected","Accepted")) df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
y y_Factor 1 1.4 Accepted 2 0.9 Rejected 3 1.0 Accepted 4 0.6 Rejected 5 1.1 Accepted 6 0.7 Rejected 7 0.9 Rejected 8 1.2 Accepted 9 0.8 Rejected 10 1.1 Accepted 11 0.7 Rejected 12 0.4 Rejected 13 1.1 Accepted 14 0.8 Rejected 15 1.1 Accepted 16 0.7 Rejected 17 1.7 Accepted 18 0.8 Rejected 19 1.1 Accepted 20 0.6 Rejected
- Related Articles
- How to create a column with binary variable based on a condition of other variable in an R data frame?
- How to add a new column in an R data frame with count based on factor column?
- How to convert a data frame into table for two factor columns and one numeric column in R?
- How to convert numeric levels of a factor into string in R data frame?
- How to create a new column in an R data frame based on some condition of another column?
- How to convert a character data frame to numeric data frame in R?
- Convert a data frame with grouping column into a list based on groups in R.
- Find the column and row names in the R data frame based on condition.
- Replace each value in a column with the largest value based on a condition in R data frame.
- How to assign a column value in a data frame based on another column in another R data frame?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to select top rows of an R data frame based on groups of factor column?
- How to subset an R data frame with condition based on only one value from categorical column?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to convert a data frame with categorical columns to numeric in R?
