- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
- Related Articles
- How to extract the first digit from a character column in an R data frame?
- How to replace zero with first non-zero occurring at the next position in an R data frame column?
- How to extract a single column of an R data frame as a data frame?
- How to extract a particular value based on index from an R data frame column?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to extract the factor levels from factor column in an R data frame?
- How to extract column names that do not have even one missing value in an R data frame?
- How to extract the closest value to a certain value in each category in an R data frame?
- Extract a particular level from factor column in an R data frame.
- How to separate first text value and the remaining text in R data frame column values?
- How to create a column of first non-zero value in each row of an R data frame?
- How to find the most frequent factor value in an R data frame column?
- How to replace zero with previous value in an R data frame column?
- Extract columns with a string in column name of an R data frame.
- How to find the first quartile for a data frame column in R?

Advertisements