# Is there any inbuilt function for calculation of mode in R

#1

I am currently trying to find the mode of a given vector in R .

``````mode(x)
[1] "numeric"
``````

But it is telling the type of object in the vector.

In R there are inbuilt function for calculation of mean and median of a vector .I want to know is there any inbuilt function for calculation of mode of a given vector

#2
``````A = c(1,2,2,3,3,3,4,5)

Mode <- function(num) {
unique_num <- unique(num)
unique_num [which.max(tabulate(match(num, unique_num )))]
}

Mode(A)
[1] 3``````

#3

Hi sid,

As per my knowledge there is no inbuilt function in R to Calculate Mode.

Many approches are there .What Sharath mentioned is also nice

Here is my approch to calulate Mode

Mode<-function(z)
{
z=na.omit(z)
t=table(z)
m=max(t)
out=as.numeric(names(t)[t==m])
return(out)
}

Mode(mtcars\$gear)
[1] 3

Hope it useful!!!

Regards,
Raghavendra

#4

hi @sid100158 ,

You can use the package “sqldf” in R as below:
library(sqldf)
gear_count <- sqldf(‘select count(*),gear from mtcars group by gear’)
This will give a nice table:

Now you can just extract the required value by:

Hope this helps!!

#5

Hi sid,

yes,there is a built in function called Mode() z der in R.

What u all have to do is use Mode() instead of mode().

Hope it will help u, instead of creating ur own function.

Regards
pragathi