In this tutorial, we will learn how to convert an R Dataframe to an R Matrix.

Consider that you have your data loaded to an R Dataframe and it is required to do some matrix operations on the data. You can load your dataframe into a matrix and do the matrix operations on it.

To convert Dataframe to Matrix in R language, use data.matrix() method. The syntax of data.matrix() method is

where frame is the dataframe and rownames.force is logical indicating if the resulting matrix should have character (rather than NULLrownames. The default, NA, uses NULL rownames if the data frame has ‘automatic’ row.names or for a zero-row data frame.

Example to Convert Dataframe to Matrix in R

In this example, we will create an R dataframe and then convert it to a matrix.