# How to convert a table into matrix in R?

To convert a table into matrix in R, we can use apply function with as.matrix.noquote function.

For example, if we have a table called TABLE then we can convert it into a matrix by using the command given below −

apply(as.matrix.noquote(TABLE),2,as.numeric)

## Example 1

Following snippet creates a dataframe −

x1<-rpois(200,20)
x1

## Output

The following dataframe is created −

[1] 26 21 18 23 24 19 19 22 21 22 23 20 36 19 17 19 20 14 18 20 18 16 30 24 27
[26] 14 19 17 24 15 29 20 26 10 24 15 27 33 19 13 14 25 26 25 20 24 23 20 21 24
[51] 21 13 30 13 22 19 15 15 21 18 14 26 24 15 14 16 25 20 18 25 28 18 14 26 22
[76] 31 17 29 18 24 15 22 17 14 25 15 16 25 27 19 19 16 24 22 14 22 17 21 18 18
[101] 24 14 20 20 34 18 13 13 23 28 16 14 16 18 29 24 21 19 17 29 24 14 12 17 20
[126] 22 21 23 16 25 16 12 19 31 19 15 17 13 16 18 19 17 18 21 23 11 19 17 16 25
[151] 19 18 19 14 25 20 18 17 15 22 19 19 19 21 15 18 17 26 22 21 16 26 17 31 25
[176] 21 18 20 16 23 16 14 16 17 23 21 11 23 14 19 16 19 24 15 19 25 19 21 16 23

To convert a table into matrix in R, add the following code to the above snippet −

x1<-rpois(200,20)
table1<-table(x1)
table1

## Output

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

x1
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33 34 36
1   2  2  6 14 11 16 14 17 23 12 14 10 10 13 11  7  3  2  4  2  3  1  1  1

To convert a table into matrix in R, add the following code to the above snippet −

x1<-rpois(200,20)
table1<-table(x1)
table1_df<-data.frame(table1)
apply(as.matrix.noquote(table1_df),2,as.numeric)

## Output

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

      x1 Freq
[1,]  10  1
[2,]  11  2
[3,]  12  2
[4,]  13  6
[5,]  14 14
[6,]  15 11
[7,]  16 16
[8,]  17 14
[9,]  18 17
[10,] 19 23
[11,] 20 12
[12,] 21 14
[13,] 22 10
[14,] 23 10
[15,] 24 13
[16,] 25 11
[17,] 26  7
[18,] 27  3
[19,] 28  2
[20,] 29  4
[21,] 30  2
[22,] 31  3
[23,] 33  1
[24,] 34  1
[25,] 36  1

## Example 2

Following snippet creates a dataframe −

x2<-rpois(200,5)
x2

The following dataframe is created −

[1] 6 7 9 4 6 6 3 6 5 3 4 3 5 5 1 4 3 3 7 4 4 9 4 3 8
[26] 6 6 5 4 7 8 3 7 4 3 5 5 6 10 9 2 4 9 9 3 5 6 5 4 14
[51] 2 8 5 6 2 7 3 9 6 6 4 6 4 4 5 6 5 6 3 5 4 8 8 3 3
[76] 8 5 5 3 9 1 4 6 0 7 4 4 3 11 5 6 4 6 4 6 2 3 4 3 9
[101] 5 0 5 4 5 2 4 6 6 5 4 4 1 2 6 4 4 3 4 6 5 3 2 9 8
[126] 4 4 5 8 4 4 5 2 1 2 6 1 3 12 3 2 7 5 7 8 7 2 3 10 2
[151] 9 8 3 4 6 1 2 3 6 4 4 5 8 6 7 8 4 5 3 4 3 7 11 9 4
[176] 2 4 2 3 5 4 5 6 7 6 12 9 4 7 7 4 6 8 3 4 6 1 6 6 4

To convert a table into matrix in R, add the following code to the above snippet −

x2<-rpois(200,5)
table2<-table(x2)
table2

## Output

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

x2
0 1  2  3  4  5  6 7  8  9  10 11 12 14
2 7 15 28 43 27 32 14 13 12 2  2  2   1

To convert a table into matrix in R, add the following code to the above snippet −

x2<-rpois(200,5)
table2<-table(x2)
table2_df<-data.frame(table2)
apply(as.matrix.noquote(table2_df),2,as.numeric)

## Output

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

      x2 Freq
[1,]  0   2
[2,]  1   7
[3,]  2  15
[4,]  3  28
[5,]  4  43
[6,]  5  27
[7,]  6  32
[8,]  7  14
[9,]  8  13
[10,] 9  12
[11,] 10  2
[12,] 11  2
[13,] 12  2
[14,] 14  1

Updated on: 06-Nov-2021

4K+ Views