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
# [1] "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

 

table 1 data frame convert array 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
# [1] "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.

 

The YouTube video will be added soon.

 

Furthermore, you could read the related articles on this homepage:

 

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.

 

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.

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

Menu
Top