# How to extract the first highest occurring value in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

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:

## Output

A
5

Let’s have a look at another example:

## Example

Live Demo

> 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
Published on 07-Nov-2020 07:38:17