How can a table be converted to a matrix in R, with an example?

How can a table be converted to a matrix in R, with an example?

A table in R can be converted to a matrix by using the “as.matrix” function. This function takes in the table as its argument and returns a matrix with the same values and dimensions as the table. For example, if we have a table named “grades” with 3 columns and 5 rows, we can convert it to a matrix by using the command “as.matrix(grades)”. This will result in a 3×5 matrix with the same values as the original table. Converting a table to a matrix can be useful for performing various operations and calculations in R.

Convert a Table to a Matrix in R (With Example)


You can use the following basic syntax to convert a table to a matrix in R:

my_matrix <- matrix(my_table, ncol=ncol(my_table), dimnames=dimnames(my_table))

The following example shows how to use this syntax in practice.

Example: Convert Table to Matrix in R

First, let’s create the following data frame in R that shows the team and position of various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 position=c('G', 'G', 'F', 'C', 'G', 'F', 'C', 'C'))

#view data frame
df

  team position
1    A        G
2    A        G
3    A        F
4    A        C
5    B        G
6    B        F
7    B        C
8    B        C

Next, let’s create a table that displays the frequency of each combination of team and position:

#create frequency table of values for team and position
my_table <- table(df$team, df$position)

#view table
my_table

    C F G
  A 1 1 2
  B 2 1 1

We can use the class() function to confirm that the object called my_table is indeed a table:

#display class of my_table
class(my_table)

[1] "table"

Next, we can use the following syntax to convert the table to a matrix:

#convert table to matrix
my_matrix <- matrix(my_table, ncol=ncol(my_table), dimnames=dimnames(my_table))

#view matrix
my_matrix

    C F G
  A 1 1 2
  B 2 1 1

And we can use the class() function to confirm that the object called my_matrix is indeed a matrix:

#display class of my_matrix
class(my_matrix)

[1] "matrix" "array"

Note #1: The ncol argument ensures that the number of columns in the matrix match the number of columns in the table.

Note #2: The dimnames argument ensures that the row names and column names match the ones from the table.

Cite this article

stats writer (2024). How can a table be converted to a matrix in R, with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-table-be-converted-to-a-matrix-in-r-with-an-example/

stats writer. "How can a table be converted to a matrix in R, with an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-table-be-converted-to-a-matrix-in-r-with-an-example/.

stats writer. "How can a table be converted to a matrix in R, with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-table-be-converted-to-a-matrix-in-r-with-an-example/.

stats writer (2024) 'How can a table be converted to a matrix in R, with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-table-be-converted-to-a-matrix-in-r-with-an-example/.

[1] stats writer, "How can a table be converted to a matrix in R, with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can a table be converted to a matrix in R, with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top