Convert Data Frame to Matrix in R (2 Examples)
This article illustrates how to change a data object from data.frame to matrix class in the R programming language.
The tutorial contains these topics:
Let’s take a look at some R codes in action…
Constructing Example Data
Have a look at the following example data.
my_df <- data.frame(x1 = 3:8, # Create data frame x2 = 10:5, x3 = 4) my_df # Print data frame # x1 x2 x3 # 1 3 10 4 # 2 4 9 4 # 3 5 8 4 # 4 6 7 4 # 5 7 6 4 # 6 8 5 4
Have a look at the previous output of the RStudio console. It shows that our example data has six rows and three columns. All variables of our data are numeric.
Let’s check the data type of our example data using the class() function in R:
class(my_df) # Class of data # "data.frame"
The class of our data object is data.frame.
Example 1: Converting Data Frame to Matrix using as.matrix() Function
This section explains how to convert a data frame to a matrix in R. For this, we can use the as.matrix function as shown below:
my_mat <- as.matrix(my_df) # Convert data frame to matrix my_mat # Print matrix # x1 x2 x3 # [1,] 3 10 4 # [2,] 4 9 4 # [3,] 5 8 4 # [4,] 6 7 4 # [5,] 7 6 4 # [6,] 8 5 4
Let’s check the class of our updated data object:
class(my_mat) # Class of data # "matrix"
Our new data object has the matrix class.
Example 2: Converting Data Frame Containing Characters and/or Factors to Numeric Matrix
In Example 1, I have explained how to convert a data frame that only contains numeric values to a matrix.
This example shows how to deal with data frames were the variables have different data formats.
Consider the following example data:
my_df_fac <- my_df # Duplicate data my_df_fac$x2 <- as.factor(my_df_fac$x2) # Convert column to factor
The values of our new example data are the same as in the previously used data frame. However, this time the column x2 is a factor.
If we apply the as.matrix function as we did in Example 1, the following output is created:
my_mat_fac <- as.matrix(my_df_fac) # Using as.matrix function my_mat_fac # Print character matrix # x1 x2 x3 # [1,] "3" "10" "4" # [2,] "4" "9" "4" # [3,] "5" "8" "4" # [4,] "6" "7" "4" # [5,] "7" "6" "4" # [6,] "8" "5" "4"
As you can see, all values in our new matrix were converted to the character class.
That must not be a bad thing. However, in case you want to create a numeric matrix you can use the apply and as.matrix.noquote functions as shown below:
my_mat_num <- apply(as.matrix.noquote(my_df_fac), # Using apply function 2, as.numeric) my_mat_num # Print numeric matrix # x1 x2 x3 # [1,] 3 10 4 # [2,] 4 9 4 # [3,] 5 8 4 # [4,] 6 7 4 # [5,] 7 6 4 # [6,] 8 5 4
The values are the same as before, but this time all values are numeric.
Video, Further Resources & Summary
Some time ago I have released a video on my YouTube channel, which explains the R code of this tutorial. You can find the video below.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might want to have a look at some of the related tutorials on this website. I have published several posts on topics such as matrices, vectors, and numeric values.
- Convert Data Frame Column to Vector in R
- Convert Data Frame Column to Numeric in R
- Change Row Names of Data Frame or Matrix
- Transpose Data Frame in R
- Introduction to R Programming
Summary: This article explained how to convert the column vectors of data frames to matrices in R. In case you have any further questions, let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter to get regular updates on the newest articles.
Statistics Globe Newsletter