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 shows that our example data consists of six rows and three columns.
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.
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.
In addition, you might have a look at the other articles on my homepage:
- Convert Values in Column into Row Names of Data Frame
- Convert Data Frame Column to Vector
- How to Compute Summary Statistics by Group
- Convert Factor to Character Class in R
- R Programming Language
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.
Statistics Globe Newsletter