- 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 find the minimum and maximum of columns values in an R data frame?
The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function but for a data frame we cannot use it directly. Check out the below examples to understand how it works.
Example1
> set.seed(974) > x1<-rpois(20,2) > x2<-rpois(20,5) > x3<-rpois(20,10) > df1<-data.frame(x1,x2,x3) > df1
Output
x1 x2 x3 1 0 6 10 2 0 7 10 3 3 3 11 4 2 7 9 5 3 2 5 6 3 4 7 7 2 7 7 8 2 8 5 9 0 4 9 10 2 2 11 11 4 7 9 12 3 9 9 13 1 2 13 14 0 1 6 15 0 4 10 16 2 3 8 17 0 8 14 18 3 5 13 19 1 4 5 20 4 5 8
Finding minimum and maximum values for columns in df1:
Example
> t(sapply(df1,range))
Output
[,1] [,2] x1 0 4 x2 1 9 x3 5 14
Example2
> y1<-sample(21:50,20) > y2<-sample(21:50,20) > y3<-sample(21:50,20) > y4<-sample(21:50,20) > y5<-sample(21:50,20) > df2<-data.frame(y1,y2,y3,y4,y5) > df2
Output
y1 y2 y3 y4 y5 1 24 46 43 27 47 2 25 24 38 33 31 3 46 44 46 39 41 4 39 23 39 47 37 5 50 42 42 37 30 6 48 39 32 44 32 7 27 31 21 40 24 8 21 27 29 41 38 9 22 30 26 23 22 10 41 28 35 30 28 11 28 32 41 34 48 12 31 37 27 43 21 13 38 34 22 29 35 14 34 50 47 22 27 15 42 47 50 35 23 16 32 49 25 49 46 17 49 33 37 42 25 18 23 22 40 48 39 19 30 48 28 45 49 20 45 45 24 25 29
Finding minimum and maximum values for columns in df2:
Example
> t(sapply(df2,range))
Output
[,1] [,2] y1 21 50 y2 22 50 y3 21 50 y4 22 49 y5 21 49
Advertisements