- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to save the summary statistics into a data frame in R?
When we find the summary statistics of a data frame then the output is returned as a table and each of the column records the minimum, first quartile, median, median, third quartile, and maximum with their names. If we want to save this summary as a data frame then it is better to calculate it with apply function and store it as data.frame.
Example
Consider the below 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
Output
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
Finding the summary of df1 −
Example
summary(df1)
Output
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
Finding the summary of df1 and saving it as a data frame in a new object −
Example
df1_summary<-as.data.frame(apply(df1,2,summary)) df1_summary
Output
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
Example
is.data.frame(df1_summary)
Output
[1] TRUE
Let’s have a look at another example −
Example
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
Output
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
Example
df2_summary<-as.data.frame(apply(df2,2,summary)) df2_summary
Output
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
Advertisements