How to replace numbers written in words with numbers in an R data frame column?



To replace numbers written in words with numbers in an R data frame column, we can use within function.

For example, if we have a data frame called df that contains a column say X having numbers from 1 to 5 written in words then we can convert them into numbers by using the below command −

within(df,X<-factor(X,labels=c(1,2,3,4,5)))

Example 1

Following snippet creates a sample data frame −

x<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(x) df1

The following dataframe is created −

     x 1  Second 2  First 3  Third 4  Third 5  Third 6  Second 7  Third 8  First 9  Second 10 Second 11 Second 12 Second 13 Second 14 Third 15 Second 16 First 17 Third 18 Third 19 First 20 Third

To replace words in x with numbers, add the following code to the above snippet −

x<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(x) within(df1,x<-factor(x,labels=c(1,2,3)))

Output

If you execute all the above given snippets as a single program, it generates the following output −

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

Example 2

Following snippet creates a sample data frame −

y<-sample(c("I","II","III","IV"),20,replace=TRUE) df2<-data.frame(y) df2

The following dataframe is created −

     y 1    I 2  III 3    I 4   IV 5    I 6    I 7  III 8   II 9    I 10   I 11 III 12 III 13  IV 14   I 15  II 16   I 17  II 18 III 19  IV 20   I

To replace Roman Numerals in y with numbers, add the following code to the above snippet −

y<-sample(c("I","II","III","IV"),20,replace=TRUE) df2<-data.frame(y) within(df2,y<-factor(y,labels=c(1,2,3,4)))

Output

If you execute all the above given snippets as a single program, it generates the following output −

   y 1  1 2  3 3  1 4  4 5  1 6  1 7  3 8  2 9  1 10 1 11 3 12 3 13 4 14 1 15 2 16 1 17 2 18 3 19 4 20 1
Updated on: 2021-11-09T07:12:44+05:30

906 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements