R Calculate The Most Element
# R - Calculate the Most Frequent Element in a Vector
[ R Language Examples](#)
In the following example, we create a custom function to find the most frequent element in a vector.
## Example
# Create a vector
getmode <-function(v){
uniqv <-unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
# Numeric vector
v <-c(2,1,2,3,1,2,3,4,1,5,5,3,2,3)
# Calculate result
result <- getmode(v)
print(result)
# Character vector
charv <-c("google","","taobao","","")
# Calculate result
result <- getmode(charv)
print(result)
Executing the above code outputs:
2 ""
[ R Language Examples](#)
YouTip