Convert summary() Function Output to Data Frame in R (Example Code)

In this tutorial, I’ll demonstrate how to transform the output of the summary() function to a data frame in the R programming language.

Example Data

data(iris)                                          # Loading example data frame
head(iris)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# 1          5.1         3.5          1.4         0.2  setosa
# 2          4.9         3.0          1.4         0.2  setosa
# 3          4.7         3.2          1.3         0.2  setosa
# 4          4.6         3.1          1.5         0.2  setosa
# 5          5.0         3.6          1.4         0.2  setosa
# 6          5.4         3.9          1.7         0.4  setosa

Example: Applying data.frame() & unclass() Functions to Convert Summary Statistics to Data Frame

iris_summary <- data.frame(unclass(summary(iris)),  # Transforming summary stats to data frame
                           check.names = FALSE)
iris_summary
#        Sepal.Length     Sepal.Width    Petal.Length     Petal.Width
# X   Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
# X.1 1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
# X.2 Median :5.800   Median :3.000   Median :4.350   Median :1.300  
# X.3 Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
# X.4 3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
# X.5 Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
#             Species
# X   setosa    :50  
# X.1 versicolor:50  
# X.2 virginica :50  
# X.3            <NA>
# X.4            <NA>
# X.5            <NA>

Further Resources

Here, you may find some further resources on topics such as data conversion and numeric values:

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top