Convert Matrix to Data Frame in R (2 Examples)

 

In this article you’ll learn how to switch from matrix to data.frame class in R.

The post will consist of the following content:

Let’s do this…

 

Introduction of Example Data

The following data is used as basement for this R tutorial:

my_mat <- matrix(1:12, nrow = 3)                        # Create example matrix
my_mat                                                  # Print example matrix
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12

As you can see based on the previous output of the RStudio console, our example data has three rows and four numeric columns.

Let’s check the data type of our example data using the class function:

class(my_mat)                                           # Class of example data
# "matrix"

Our example data has the matrix class.

 

Example 1: Converting Matrix to Data Frame Using as.data.frame() Function

In Example 1, I’ll illustrate how to convert a matrix to a data frame in R. For this, we can use the as.data.frame function as shown below:

my_data <- as.data.frame(my_mat)                        # Convert matrix to data.frame
my_data                                                 # Print data frame
#   V1 V2 V3 V4
# 1  1  4  7 10
# 2  2  5  8 11
# 3  3  6  9 12

Have a look at the previous output of our data shown in the RStudio console. As you can see, the values of the data are the same. However, the row and column names were changed.

Let’s check the class of our updated data object:

class(my_data)                                          # Class of converted data
# "data.frame"

Our new data object has the data.frame class!

 

Example 2: Changing Column & Row Names of Data Frame

As discussed in Example 1, the row and column names were automatically changed by the as.data.frame function. Example 2 illustrates how to modify the row and column names of our data frame manually.

First, we’ll rename the column names of our data frame using the colnames function:

colnames(my_data) <- c("Col1", "Col2", "Col3", "Col4")  # Modify column names
my_data                                                 # Print updated data
#   Col1 Col2 Col3 Col4
# 1    1    4    7   10
# 2    2    5    8   11
# 3    3    6    9   12

As you can see based on the previous RStudio console output, our column names were modified.

Now, let’s change the row names of our data by applying the rownames function:

rownames(my_data) <- c("Row1", "Row2", "Row3")          # Modify row names
my_data                                                 # Print updated data
#      Col1 Col2 Col3 Col4
# Row1    1    4    7   10
# Row2    2    5    8   11
# Row3    3    6    9   12

Looks good!

 

Video & Further Resources

If you need further explanations on the examples of this article, you could watch the following video of my YouTube channel. In the video, I illustrate the R programming code of this post in RStudio:

 

 

Furthermore, I can recommend to read some of the related R articles on my homepage. Some articles about similar topics such as lists, data objects, and time objects are shown below.

 

To summarize: In this tutorial you learned how to convert matrices to data frames in the R programming language. In case you have further questions, please let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top