如何在R中将汇总统计数据保存到数据框中?
当我们找到数据框的汇总统计数据时,输出将作为表格返回,每一列记录最小值、第一四分位数、中位数、中位数、第三四分位数和最大值及其名称。如果我们想将此摘要保存为数据框,最好使用apply函数计算它并将其存储为data.frame。
示例
考虑下面的数据框:
x1<-rpois(20,5) x2<-rexp(20,2) x3<-rexp(20,5) x4<-runif(20,5,10) x5<-runif(20,5,12) df1<-data.frame(x1,x2,x3,x4,x5) df1
输出
x1 x2 x3 x4 x5 1 10 0.318955383 0.021788087 6.418559 8.407760 2 2 0.682127794 0.354887266 7.915153 7.691196 3 8 0.093838493 0.750094498 5.825129 5.047835 4 7 0.298823558 0.008525539 5.481506 5.025790 5 7 0.031303249 0.491977567 7.143065 11.964555 6 4 0.125743637 0.165031313 6.778808 5.755208 7 2 0.245636217 0.274977357 9.224668 7.930684 8 4 1.222748429 0.034911250 6.300662 10.025192 9 4 0.447608813 0.122677772 5.115722 10.197774 10 7 0.114562157 0.400451206 9.311998 11.103992 11 4 0.252932058 0.200755263 6.672940 9.255076 12 4 0.164882561 0.085901924 8.158944 10.293423 13 6 0.236620346 0.132488792 7.732131 10.930689 14 7 0.019540590 0.076331686 6.882222 9.289458 15 5 0.002908304 0.008934306 5.929365 10.552569 16 6 0.547663136 0.350376081 7.144703 5.159983 17 2 0.345556123 0.144144203 8.153868 7.918402 18 3 0.306435164 0.053920204 7.604212 11.124177 19 9 1.121258744 0.015824366 8.298107 9.531429 20 6 1.139374780 0.301424552 8.646805 11.471353
查找df1的摘要:
示例
summary(df1)
输出
x1 x2 x3 x4 Min. : 2.00 Min. :0.002908 Min. :0.008526 Min. :5.116 1st Qu.: 4.00 1st Qu.:0.122948 1st Qu.:0.049168 1st Qu.:6.389 Median : 5.50 Median :0.275878 Median :0.138317 Median :7.144 Mean : 5.35 Mean :0.385926 Mean :0.199771 Mean :7.237 3rd Qu.: 7.00 3rd Qu.:0.472622 3rd Qu.:0.313662 3rd Qu.:8.155 Max. :10.00 Max. :1.222748 Max. :0.750094 Max. :9.312 x5 Min. : 5.026 1st Qu.: 7.862 Median : 9.410 Mean : 8.934 3rd Qu.:10.647 Max. :11.965
查找df1的摘要并将其作为数据框保存到新的对象中:
示例
df1_summary<-as.data.frame(apply(df1,2,summary)) df1_summary
输出
x1 x2 x3 x4 x5 Min. 2.00 0.002908304 0.008525539 5.115722 5.025790 1st Qu. 4.00 0.122948267 0.049167965 6.389085 7.861600 Median 5.50 0.275877808 0.138316497 7.143884 9.410443 Mean 5.35 0.385925977 0.199771162 7.236928 8.933827 3rd Qu. 7.00 0.472622394 0.313662434 8.155137 10.647099 Max. 10.00 1.222748429 0.750094498 9.311998 11.964555
示例
is.data.frame(df1_summary)
输出
[1] TRUE
让我们来看另一个例子:
示例
y1<-sample(1:100,20) y2<-sample(1:10,20,replace=TRUE) y3<-sample(20:100,20,replace=TRUE) y4<-sample(50:100,20,replace=TRUE) y5<-rpois(20,15) df2<-data.frame(y1,y2,y3,y4,y5) df2
输出
y1 y2 y3 y4 y5 1 23 6 64 52 17 2 90 2 95 93 17 3 45 8 44 95 14 4 8 10 32 86 10 5 79 8 29 63 14 6 36 9 60 77 16 7 67 8 31 55 16 8 39 9 27 58 11 9 33 9 90 82 15 10 38 4 34 95 11 11 99 1 68 68 19 12 28 3 58 86 6 13 81 8 54 83 16 14 87 2 25 50 20 15 53 1 90 77 10 16 10 9 23 79 14 17 41 7 93 53 12 18 97 7 100 75 17 19 1 4 67 60 15 20 80 7 27 54 17
示例
df2_summary<-as.data.frame(apply(df2,2,summary)) df2_summary
输出
y1 y2 y3 y4 y5 Min. 1.00 1.00 23.00 50.00 6.00 1st Qu. 31.75 3.75 30.50 57.25 11.75 Median 43.00 7.00 56.00 76.00 15.00 Mean 51.75 6.10 55.55 72.05 14.35 3rd Qu. 80.25 8.25 73.50 83.75 17.00 Max. 99.00 10.00 100.00 95.00 20.00
广告