- 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 extract the first highest occurring value in an R data frame column?
The highest occurring value is called the mode and there can be multiple modes in a variable. If we have multiple modes then we can find the first mode or first highest occurring value by using sort function. For example, if we have a vector x that contains more than two modes then the first mode can be found as:
sort(table(df$x),decreasing=TRUE)[1]
Example
Consider the below data frame:
> set.seed(36521) > x<-sample(LETTERS[1:5],20,replace=TRUE) > df1<-data.frame(x) > df1
Output
x 1 B 2 E 3 A 4 A 5 D 6 E 7 D 8 B 9 B 10 C 11 E 12 D 13 E 14 A 15 A 16 A 17 C 18 B 19 D 20 D
Finding the first mode in x:
> sort(table(df1$x),decreasing=TRUE)[1]
Output
A 5
Let’s have a look at another example:
Example
> y<-rpois(20,5) > df2<-data.frame(y) > df2
Output
y 1 5 2 7 3 4 4 7 5 1 6 4 7 7 8 10 9 4 10 4 11 6 12 5 13 6 14 5 15 4 16 2 17 4 18 6 19 5 20 1
Finding the first mode in y:
> sort(table(df2$y),decreasing=TRUE)[1]
Output
4 6
Advertisements