Restructure output of R summary function


 Treat it as a data.frame:

set.seed(1)
x <- sample(30, 100, TRUE)

summary(x)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
#    1.00   10.00   15.00   16.03   23.25   30.00
summary(data.frame(x))
#        x        
#  Min.   : 1.00  
#  1st Qu.:10.00  
#  Median :15.00  
#  Mean   :16.03  
#  3rd Qu.:23.25  
#  Max.   :30.00

For slightly more usable output, you can use data.frame(unclass(.)):
val = data.frame(unclass(summary(x)))
val
#           val
# Min.     1.00
# 1st Qu. 10.00
# Median  15.00
# Mean    16.03
# 3rd Qu. 23.25
# Max.    30.00

val[1,1]
val[2,1]
val[3,1]

Or you can use stack:

stack(summary(x))
#   values     ind
# 1   1.00    Min.
# 2  10.00 1st Qu.
# 3  15.00  Median
# 4  16.03    Mean
# 5  23.25 3rd Qu.
# 6  30.00    Max.

Ref:

https://stackoverflow.com/questions/27715934/restructure-output-of-r-summary-function

posted @ 2018-06-30 02:21 emanlee 阅读(...) 评论(...) 编辑 收藏