如何按照 R 中的垂直順序顯示資料框摘要?


要垂直顯示資料框摘要,可以使用 lapply 和 cbind 函式以及 summary 函式。例如,如果有一個稱為 df 的資料框,則可以透過使用以下命令以垂直順序查詢 df 的摘要 -

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

示例 1

考慮以下資料框 -

線上演示

> 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

輸出

   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 

按照垂直順序查詢 df1 的摘要 -

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

輸出

$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

示例 2

線上演示

> 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

輸出

         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

按照垂直順序查詢 df2 的摘要 -

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

輸出

$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

更新於: 2021 年 3 月 6 日

350 次瀏覽

開啟您的職業生涯

完成該課程即可獲得認證

開始
廣告
© . All rights reserved.