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
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
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:
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.
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.
- Convert Data Frame Rows to List
- Convert Named Vector to Data Frame
- Convert Data Frame with Date Column to Time Series Object
- Change Row Names of Data Frame or Matrix
- R Programming Overview
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.
Statistics Globe Newsletter