- 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
Set values in categorical column to numeric values in R data frame.
To set values in categorical column to numeric values in R data frame, we can use combine function c.
For Example, if we have a data frame called df that contains a categorical column say C which has two categories as Low and High and if we want to represent these categories with 1 and 10 then we can use the below command −
df$C<-c(Low=1,High=10)[df$C]
Example 1
Following snippet creates a sample data frame −
Group<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(Group) df1
The following dataframe is created
Group 1 Third 2 Second 3 First 4 Second 5 Second 6 First 7 Third 8 Third 9 First 10 Second 11 First 12 First 13 First 14 Second 15 First 16 First 17 First 18 Second 19 Second 20 Third
To replace values in Group column to numeric on the above created data frame, add the following code to the above snippet −
Group<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(Group) df1$Group<-c(First=1,Second=2,Third=3)[df1$Group] df1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Group 1 3 2 2 3 1 4 2 5 2 6 1 7 3 8 3 9 1 10 2 11 1 12 1 13 1 14 2 15 1 16 1 17 1 18 2 19 2 20 3
Example 2
Following snippet creates a sample data frame −
Level<-sample(c("Low","Medium","High"),20,replace=TRUE) df2<-data.frame(Level) df2
The following dataframe is created
Level 1 Medium 2 Low 3 Low 4 High 5 Medium 6 Medium 7 Medium 8 Medium 9 Low 10 Low 11 Low 12 Low 13 Low 14 High 15 Medium 16 Medium 17 Low 18 Low 19 Medium 20 Medium
To replace values in Level column to numeric on the above created data frame, add the following code to the above snippet −
Level<-sample(c("Low","Medium","High"),20,replace=TRUE) df2<-data.frame(Level) df2$Level<-c(Low=5,Medium=15,High=20)[df2$Level] df2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Level 1 15 2 5 3 5 4 20 5 15 6 15 7 15 8 15 9 5 10 5 11 5 12 5 13 5 14 20 15 15 16 15 17 5 18 5 19 15 20 15
- Related Articles
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- How to repeat column values in R data frame by values in another column?
- Replace numerical column values based on character column values in R data frame.
- How to subtract column values from column means in R data frame?
- How to change row values based on column values in an R data frame?
- How to fill NA values with previous values in an R data frame column?
- How to convert a data frame with categorical columns to numeric in R?
- How to select positive values in an R data frame column?
- How to randomly replace values in an R data frame column?
- How to set NA values to TRUE for a Boolean column in an R data frame?
- How to separate two values in single column in R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to convert values in alternate rows to negative in R data frame column?
- How to check if a column is categorical in R data frame?
- How to replace missing values with median in an R data frame column?
