Home » How to Convert Data Frame to Matrix in R (With Examples)

How to Convert Data Frame to Matrix in R (With Examples)

by Erma Khan
spot_img

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 matrix(df)

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

mat 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 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 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 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 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

The following tutorials explain how to perform other common operations in R:

How to Convert Data Frame Column to Vector in R
How to Convert Matrix to Vector in R
How to Convert Table to Data Frame in R

spot_img

Related Posts