Convert Array to Data Frame in R (Example)
In this article you’ll learn how to reshape an array to a data frame in the R programming language.
Table of contents:
You’re here for the answer, so let’s get straight to the example:
Creating Example Data
Have a look at the example data below:
my_array <- array(1:12, # Create example array dim = c(3, 1, 4)) my_array # Print example array # , , 1 # # [,1] # [1,] 1 # [2,] 2 # [3,] 3 # # , , 2 # # [,1] # [1,] 4 # [2,] 5 # [3,] 6 # # , , 3 # # [,1] # [1,] 7 # [2,] 8 # [3,] 9 # # , , 4 # # [,1] # [1,] 10 # [2,] 11 # [3,] 12
Have a look at the previous output of the RStudio console. It shows that our example data is an array object containing four different items.
We can confirm this by applying the class function to our array:
class(my_array) # Check class of example array #  "array"
As you can see, the RStudio console confirms that our data object has the array data type.
Example: Reshape Array to Data Frame Object Using as.data.frame.table() Function
This example shows how to transform an array object to a data frame in the R programming language.
For this task, we can apply the as.data.frame.table function as shown in the following R syntax:
my_data <- as.data.frame.table(my_array) # Convert array to data frame my_data # Print data frame
The previous table shows the result of the R code above. As you can see, we have reshaped our array object to a data frame.
Let’s check the class of our new data object:
class(my_data) # Check class of data frame #  "data.frame"
As expected, we have created a data object with the data.frame class.
Video, Further Resources & Summary
Do you need more explanations on the R programming codes of this tutorial? Then I recommend watching the following video on my YouTube channel. I’m explaining the examples of this page in the video.
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, you could read the related articles on this homepage:
- Convert Data Frame to Array in R
- Convert Data Frame Column to Vector
- Convert Data Frame Rows to List
- Convert Data Frame with Date Column to Time Series Object
- Convert Named Vector to Data Frame in R
- Introduction to R
This tutorial has demonstrated how to convert an array to a data frame object in R programming. Tell me about it in the comments section, in case you have further questions.
Statistics Globe Newsletter