How can I convert a data frame to a matrix in R?

How can I convert a data frame to a matrix in R?

Converting a data frame to a matrix in R involves transforming the tabular data structure of a data frame into a two-dimensional array of values. This can be achieved by using the “as.matrix()” function in R, which converts the data frame to a matrix while maintaining the column and row names. This conversion is useful for performing certain operations and calculations that are more efficient and easier to implement on matrices compared to data frames. Additionally, it allows for easier manipulation and analysis of data, making it a valuable tool in data analysis using R.

Convert Data Frame to Matrix in R (With Examples)


You can use one of the following methods to convert a data frame to a matrix in R:

Method 1: Convert Data Frame of Numeric Columns to Matrix

mat <- as.matrix(df)

Method 2: Convert Data Frame with Characters / Factors to Matrix

mat <- data.matrix(df)

Note that both methods use functions from base R, so you don’t have to install any external packages to use these methods.

The following examples show how to use each method in practice.

Method 1: Convert Data Frame of Numeric Columns to Matrix

Suppose we have the following data frame in R that only contains numeric columns:

#create data frame
df <- data.frame(points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  points assists rebounds
1     99      33       30
2     90      28       28
3     86      31       24
4     88      39       24
5     95      34       28

We can use the as.matrix() function to quickly convert this data frame to a numeric matrix:

#convert data frame to matrix
mat <- as.matrix(df)

#view matrix
mat

     points assists rebounds
[1,]     99      33       30
[2,]     90      28       28
[3,]     86      31       24
[4,]     88      39       24
[5,]     95      34       28

#view class of mat
class(mat)

[1] "matrix" "array"

By using the class() function, we confirm that the new object is indeed a matrix.

Method 2: Convert Data Frame with Characters / Factors to Matrix

Suppose we have the following data frame in R that contains both character and numeric columns:

#create data frame
df <- data.frame(team=c('A', 'A', 'B', 'B', 'C'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34))

#view data frame
df

  team points assists
1    A     99      33
2    A     90      28
3    B     86      31
4    B     88      39
5    C     95      34

We can use the data.matrix() function to quickly convert this data frame to a numeric matrix:

#convert data frame to matrix
mat <- data.matrix(df)

#view matrix
mat

     team points assists
[1,]    1     99      33
[2,]    1     90      28
[3,]    2     86      31
[4,]    2     88      39
[5,]    3     95      34

#view class of mat
class(mat)

[1] "matrix" "array"

By using the class() function, we confirm that the new object is indeed a matrix.

We can also type the following:

?data.matrix

Which tells us:

Description:
     Return the matrix obtained by converting all the variables in a
     data frame to numeric mode and then binding them together as the
     columns of a matrix.  Factors and ordered factors are replaced by
     their internal codes.

This explains why the team names A, A, B, B, C were converted to the values 1, 1, 2, 2, 3.

Additional Resources

Cite this article

stats writer (2024). How can I convert a data frame to a matrix in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-a-data-frame-to-a-matrix-in-r/

stats writer. "How can I convert a data frame to a matrix in R?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-a-data-frame-to-a-matrix-in-r/.

stats writer. "How can I convert a data frame to a matrix in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-a-data-frame-to-a-matrix-in-r/.

stats writer (2024) 'How can I convert a data frame to a matrix in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-a-data-frame-to-a-matrix-in-r/.

[1] stats writer, "How can I convert a data frame to a matrix in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I convert a data frame to a matrix in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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