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**

To calculate mode in R, you have to define the user-defined function. In R, mean() and median() are standard functions that do what you’d 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.