Convert Summary Statistics to Data Frame in R (Example)

 

In this article you’ll learn how to convert the output of the summary() function to a data frame in the R programming language.

The article contains the following content:

Let’s take a look at some R codes in action:

 

Creation of Example Data

Let’s first create some example data in R.

data <- data.frame(x1 = 6:1,                        # Create example data frame
                   x2 = 6:11,
                   x3 = letters[2:7])
data                                                # Print example data frame

 

table 1 data frame convert summary statistics data frame r

 

Table 1 shows that our example data consists of six rows and three columns.

Next, let’s apply the summary function to this data frame to return certain descriptive and summary statistics.

summary(data)                                       # Summary statistics of data frame
#        x1             x2             x3           
#  Min.   :1.00   Min.   : 6.00   Length:6          
#  1st Qu.:2.25   1st Qu.: 7.25   Class :character  
#  Median :3.50   Median : 8.50   Mode  :character  
#  Mean   :3.50   Mean   : 8.50                     
#  3rd Qu.:4.75   3rd Qu.: 9.75                     
#  Max.   :6.00   Max.   :11.00

Looks good. However, it might be useful to convert this output to data frame to be able to handle its values better.

In the following example, I’ll show how to do that. So keep on reading!

 

Example: Convert summary() Function Output to Data Frame Using data.frame() & unclass() Functions

This example explains how to convert a summary statistics output to the data.frame class in the R programming language.

For this task, we can use the data.frame, unclass, and summary functions as shown below:

data_summary <- data.frame(unclass(summary(data)),  # Convert summary to data frame
                           check.names = FALSE)
data_summary                                        # Print summary as data frame
#                 x1              x2                 x3
# X   Min.   :1.00   Min.   : 6.00   Length:6          
# X.1 1st Qu.:2.25   1st Qu.: 7.25   Class :character  
# X.2 Median :3.50   Median : 8.50   Mode  :character  
# X.3 Mean   :3.50   Mean   : 8.50                 <NA>
# X.4 3rd Qu.:4.75   3rd Qu.: 9.75                 <NA>
# X.5 Max.   :6.00   Max.   :11.00                 <NA>

By running the previous R code, we have created a data frame called data_summary, which contains our summary statistics output.

 

Video & Further Resources

Do you want to know more about the transformation of the summary() function output to a data frame? Then I recommend watching the following video on my YouTube channel. I’m illustrating the R code of this post in the video.

 

 

In addition, you might have a look at the other articles on my homepage:

 

In this tutorial you have learned how to transform the summary() function output to a data frame in the R programming language. Don’t hesitate to let me know in the comments below, if you have any additional questions or 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.


2 Comments. Leave new

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