The mode in R is the value with the highest number of occurrences in a set of data. Unlike the **mean** and **median**, the mode can have both numeric and character data.

**Mode in R**

There is no built-in function to calculate the mode in R. To calculate mode in R, you have to create the user-defined function that returns the mode using mathematical computation.

In R, mean() and median() are standard functions that do what you would expect. This is because the mode tells you the internal storage mode of the object, not the value that occurs the most in its argument.

Let’s calculate the mode of Vector in R.

```
# Create a vector.
rv <- c(11, 18, 19, 21, 29, 46, 21)
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
result <- getmode(rv)
print(result)
```

**Output**

`[1] 21`

If you see the vector elements, you can see that 21 is the only value that is repeated; that is why it returns 21.

Let’s see another example.

```
# Create a vector.
rv <- c(11, 18, 11, 19, 21, 19, 46, 21, 19)
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
result <- getmode(rv)
print(result)
```

**Output**

`[1] 19`

Here 19 occurs multiple times, so it will be considered the mode of the given sequence.

**Conclusion**

To calculate mode in R, we have created a user-defined function to find the mode of a data set in R. Here, the function takes the vector as an input and calculates the mode value as the output.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.