How to convert character column of a matrix into numeric in R?

R ProgrammingServer Side ProgrammingProgramming

If we have a matrix that contains character columns and we want to convert a single column to numeric then we first need to convert the matrix into a data frame using as.data.frame function after that as.numeric function can be used to change the particular column to numeric type as shown in the below examples.

Example 1

Following snippet creates a matrix −

M1<-matrix(factor(rpois(25,5)),ncol=5)
M1

The following matrix is created −

     [,1][,2][,3][,4][,5]
[1,] "1" "2" "9" "8"  "8"
[2,] "8" "5" "5" "9"  "6"
[3,] "6" "5" "5" "2"  "6"
[4,] "4" "2" "4" "8"  "5"
[5,] "2" "5" "6" "5" "10"

Add the following code to the above snippet −

M1<-matrix(factor(rpois(25,5)),ncol=5)
M1<-as.data.frame(M1)
M1[,1]<-as.numeric(M1[,1])
M1

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

   V1 V2 V3 V4 V5
1  1  2  9  8  8
2  8  5  5  9  6
3  6  5  5  2  6
4  4  2  4  8  5
5  2  5  6  5 10

To convert character column of a matrix into numeric in R, add the following code to the above snippet −

M1<-matrix(factor(rpois(25,5)),ncol=5)
M1<-as.data.frame(M1)
M1[,1]<-as.numeric(M1[,1])
str(M1)

Output

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

'data.frame': 5 obs. of 5 variables:
$ V1: num 1 8 6 4 2
$ V2: chr "2" "5" "5" "2" ...
$ V3: chr "9" "5" "5" "4" ...
$ V4: chr "8" "9" "2" "8" ...
$ V5: chr "8" "6" "6" "5" ...

Example 2

Following snippet creates a matrix −

M2<-matrix(factor(rpois(80,10)),ncol=4)
M2

The following matrix is created −

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

Add the following code to the above snippet −

M2<-matrix(factor(rpois(80,10)),ncol=4)
M2<-as.data.frame(M2)
M2[,4]<-as.numeric(M2[,4])
M2

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

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

To convert character column of a matrix into numeric in R, add the following code to the above snippet −

M2<-matrix(factor(rpois(80,10)),ncol=4)
M2<-as.data.frame(M2)
M2[,4]<-as.numeric(M2[,4])
str(M2)

Output

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

'data.frame': 20 obs. of 4 variables:
$ V1: chr "14" "10" "5" "10" ...
$ V2: chr "18" "8" "7" "9" ...
$ V3: chr "20" "9" "10" "12" ...
$ V4: num 7 13 9 12 5 9 10 17 7 17 ...

Example 3

Following snippet creates a matrix −

M3<-matrix(factor(rpois(100,5)),ncol=5)
M3

The following matrix is created −

     [,1] [,2] [,3] [,4] [,5]
[1,]  "1" "5" "3"   "5"   "6"
[2,]  "4" "5" "8"  "10"  "10"
[3,]  "6" "3" "5"   "2"   "4"
[4,]  "7" "5" "3"   "4"   "5"
[5,]  "5" "4" "5"   "8"   "3"
[6,]  "4" "5" "8"   "3"   "8"
[7,]  "3" "5" "0"   "4"   "5"
[8,] "11" "10" "1"  "8"   "2"
[9,]  "4" "4" "5"   "3"   "5"
[10,] "8" "7" "3"   "3"   "7"
[11,] "5" "6" "5"   "6"   "3"
[12,] "8" "12" "6"  "4"   "4"
[13,] "5" "6" "5"   "9"   "5"
[14,] "9" "7" "3"   "5"   "2"
[15,] "6" "4" "7"   "6"   "4"
[16,] "6" "4" "4"   "4"   "7"
[17,] "4" "3" "3"   "4"   "5"
[18,] "2" "4" "3"   "2"   "2"
[19,] "3" "4" "3"   "6"   "4"
[20,] "6" "8" "6"   "6"   "6"

Add the following code to the above snippet −

M3<-matrix(factor(rpois(100,5)),ncol=5)
M3<-as.data.frame(M3)
M3[,1]<-as.numeric(M3[,1])
M3

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

    V1 V2 V3 V4 V5
1   1  5  3  5  6
2   4  5  8 10 10
3   6  3  5  2  4
4   7  5  3  4  5
5   5  4  5  8  3
6   4  5  8  3  8 
7   3  5  0  4  5
8  11 10  1  8  2
9   4  4  5  3  5
10  8  7  3  3  7
11  5  6  5  6  3
12  8 12  6  4  4
13  5  6  5  9  5
14  9  7  3  5  2
15  6  4  7  6  4
16  6  4  4  4  7
17  4  3  3  4  5
18  2  4  3  2  2
19  3  4  3  6  4
20  6  8  6  6  6

To convert character column of a matrix into numeric in R, add the following code to the above snippet −

M3<-matrix(factor(rpois(100,5)),ncol=5)
M3<-as.data.frame(M3)
M3[,1]<-as.numeric(M3[,1])
str(M3)

Output

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

'data.frame': 20 obs. of 5 variables:
$ V1: num 1 4 6 7 5 4 3 11 4 8 ...
$ V2: chr "5" "5" "3" "5" ...
$ V3: chr "3" "8" "5" "3" ...
$ V4: chr "5" "10" "2" "4" ...
$ V5: chr "6" "10" "4" "5" ...
raja
Updated on 22-Nov-2021 07:19:54

Advertisements