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
Updated on: 2021-11-03T07:55:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements