How to convert number to words in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To convert number to words in an R data frame column, we can use english function from english package. For example, if we have a data frame called df that contains a number column x then we can convert the numbers into words by using the command as.character(english(df$x)).

Example

Consider the below data frame −

 Live Demo

x<-rpois(20,5)
df1<-data.frame(x)
df1

Output

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

Loading english package and converting numbers in column x to words −

Example

library(english)
df1$x<-as.character(english(df1$x))
df1

Output

     x
1   three
2   five
3   four
4   seven
5   seven
6   three
7   six
8   one
9   eleven
10  six
11  six
12  six
13  five
14  seven
15  four
16  one
17  three
18  one
19  one
20  one

Example

 Live Demo

y<-rpois(20,10)
df2<-data.frame(y)
df2

Output

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

Converting numbers in column y to words −

Example

df2$y<-as.character(english(df2$y))
df2

Output

     y
1   six
2   twelve
3   ten
4   eleven
5   thirteen
6   five
7   seven
8   eight
9   two
10  eleven
11  eleven
12  eleven
13  twelve
14  thirteen
15  fifteen
16  six
17  eleven
18  six
19  eleven
20  ten
raja
Published on 16-Mar-2021 10:18:41
Advertisements