Home » R Tutorials » Convert a Data Frame to Matrix in R

Convert a Data Frame to Matrix in R

data.matrix() and as.matrix() functions in R is used to convert a data frame to matrix. The data.matrix() function converts all data frame variables to numeric and displays them as columns of the matrix. The as.matrix() is a generic function and returns a character matrix.

In this tutorial, we will discuss how to convert a data frame to a matrix using the data.matrix and as.matrix method.

Convert a Data Frame to Matrix using as.matrix

as.matrix function is generic and converts data frame data into a character matrix.

as.matrix syntax:

as.matrix(x, ...)

where x is R object

Let’s understand converting a data frame to a matrix using the as.matrix function.

Let’s create a student_info data frame. It has a name, age, gender, and marks columns.

# Create a data frame
student_info <- data.frame(
  name = c("Tom","Kim","Sam","Julie","Emily","Chris"),
  age = c(20,21,19,20,21,22),
  gender = c('M','F','M','F','F','M'),
  marks = c(72,77,65,80,85,87)
 
)
# Print the data frame
student_info

To convert a data frame to the matrix using as.matrix, use the following code.

# Convert a data frame to character matrix using as.matrix
data <- as.matrix(student_info)
# Print the data frame
data

In the above R code, as.matrix takes the data frame as input parameter and converts a data frame to a character matrix.

The output of the above R code is:

     name    age  gender marks
[1,] "Tom"   "20" "M"    "72" 
[2,] "Kim"   "21" "F"    "77" 
[3,] "Sam"   "19" "M"    "65" 
[4,] "Julie" "20" "F"    "80" 
[5,] "Emily" "21" "F"    "85" 
[6,] "Chris" "22" "M"    "87" 

Convert a Data Frame to a Numeric Matrix using data.matrix

data.matrix function in R converts data frame to the numeric matrix.

data.matrix syntax:

data.matrix(frame, rownames.force = NA)

Where,
frame = data frame
rownames.force = logical indicating if output matrix should have character (rather than NULL)

If the data frame contains logical or factor columns, it will be converted to an integer. Character columns are converted to factors and later converted to an integer.

Let’s understand converting a data frame to a numeric matrix using data.matrix function with an example.

Use the above student_info data frame to understand the conversion of a data frame to the matrix.

# Convert a data frame to numeric matrix using data.matrix function
matrix_data <- data.matrix(student_info)
# Print the data frame
matrix_data

The output of the above R code is:

     name age gender marks
[1,]    6  20      2    72
[2,]    4  21      1    77
[3,]    5  19      2    65
[4,]    3  20      1    80
[5,]    2  21      1    85
[6,]    1  22      2    87

In the above resultant matrix, data.matrix converts character columns such as name and gender to factor and later factor to an integer.

data.matrix converts data frame to the numeric matrix.

Conclusion

I hope the above article on how to convert a data frame to a matrix is helpful to you.

Use as.matrix() function to convert a data frame to the character matrix and data.matrix() function for converting a data frame to a numeric matrix.

Leave a Comment