How to display the data frame summary in vertical order in R?


To display the data frame summary in vertical order, we can use lapply and cbind function along with the summary function. For example, if we have a data frame called df then the summary of df in vertical order can be found by using the below command −

lapply(df,function(x) cbind(summary(x)))

Example1

Consider the below data frame −

Live Demo

> x1<-rpois(20,5)
> x2<-rpois(20,2)
> x3<-rpois(20,3)
> x4<-rpois(20,3)
> x5<-rpois(20,5)
> df1<-data.frame(x1,x2,x3,x4,x5)
> df1

Output

   x1 x2 x3 x4 x5 1   4  0  2  2  6 2   7  2  4  1  7 3   7  2  3  3  6 4   4  0  4  5  2 5   5  2  2  1  8 6   2  1  2  9  5 7   4  0  2  6  6 8   5  2  1  5  5 9   3  3  2  2 10 10  5  2  1  4  3 11  4  2  3  4  6 12  3  4  5  2  6 13  6  1  1  2 10 14  3  2  3  5  9 15  2  1  0  1  8 16  2  2  4  5  7 17  5  1  3  3  5 18  4  6  5  1  3 19 10  2  4  3  6 20  2  1  4  3  7 

Finding the summary of df1 in vertical order −

> lapply(df1,function(x) cbind(summary(x)))

Output

$x1
         [,1]
Min.     2.00
1st Qu.  3.00
Median   4.00
Mean     4.35
3rd Qu.  5.00
Max.    10.00
 
$x2
        [,1]
Min.     0.0
1st Qu.  1.0
Median   2.0
Mean     1.8
3rd Qu.  2.0
Max.     6.0
 
$x3
        [,1]
Min.    0.00
1st Qu. 2.00
Median  3.00
Mean    2.75
3rd Qu. 4.00
Max.    5.00
 
$x4
        [,1]
Min.    1.00
1st Qu. 2.00
Median  3.00
Mean    3.35
3rd Qu. 5.00
Max.    9.00
 
$x5
         [,1]
Min.     2.00
1st Qu.  5.00
Median   6.00
Mean     6.25
3rd Qu.  7.25
Max.    10.00

Example2

Live Demo

> y1<-rnorm(20,21,2.25)
> y2<-rnorm(20,252,37.24)
> y3<-rnorm(20,25457,3654.25)
> df2<-data.frame(y1,y2,y3)
> df2

Output

         y1       y2       y3
1  19.85077 283.9417 27360.27
2  22.30740 224.1180 21955.95
3  22.82174 289.0184 29019.13
4  23.01813 197.2612 20215.58
5  20.14611 272.0723 22895.22
6  18.06886 160.0688 27238.66
7  24.64121 213.4816 21500.75
8  18.02140 306.3236 26131.44
9  20.35991 230.8819 24885.17
10 18.23664 226.1749 28375.36
11 25.54674 234.6761 22919.52
12 19.24247 239.6695 23611.60
13 23.25991 273.1509 25088.87
14 17.31507 172.9442 20658.12
15 17.38401 269.1130 20928.33
16 20.18348 256.1337 21907.11
17 21.77760 210.4990 17576.79
18 21.64395 304.7984 19382.12
19 20.19686 214.9300 22940.67
20 21.81332 279.9707 19274.34

Finding the summary of df2 in vertical order −

> lapply(df2,function(x) cbind(summary(x)))

Output

$y1
            [,1]
Min.    17.31507
1st Qu. 18.99101
Median  20.27838
Mean    20.79178
3rd Qu. 22.43599
Max.    25.54674
 
$y2
            [,1]
Min.    160.0688
1st Qu. 214.5679
Median  237.1728
Mean    242.9614
3rd Qu. 274.8559
Max.    306.3236
 
$y3
            [,1]
Min.    17576.79
1st Qu. 20860.78
Median  22907.37
Mean    23193.25
3rd Qu. 25349.52
Max.    29019.13

Updated on: 06-Mar-2021

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements