
上QQ阅读APP看书,第一时间看更新
There's more...
One common scenario in univariate descriptive statistics is to find the mode of a vector. In R, there is no built-in function to help the user obtain the mode of the data. However, one can implement the mode function by using the following code:
> mode = function(x) {
+ temp = table(x)
+ names(temp)[temp == max(temp)]
+ }
By applying the mode function on the vector, mtcars$mpg, you can find the most frequently occurring numeric value or category of a given vector:
> x = c(1,2,3,3,3,4,4,5,5,5,6)
> mode(x)
Output:
[1] "3" "5"