statamode | R Documentation |
This function mimics the functionality of the mode function in Stata. It does this by calculating the modal category of a vector and replacing tied categories with a "."to represent a single mode does not exist.
statamode(x, method = c("last", "stata", "sample"))
x |
a vector, missing values are allowed |
method |
a character vector of length 1 specifying the way to break ties in cases where more than one mode exists; either "stata", "sample", or "last". "stata" provides a "." if more than one mode exists. "sample" randomly samples from among the tied values for a single mode. "last" takes the final modal category appearing in the data. |
Specifying method="stata" will result in ties for the mode being replaced with a "." character. Specifying "sample" will result in the function randomly sampling among the tied values and picking a single value. Finally, specifying "last" will result in the function picking the value that appears last in the original x vector. The default behavior is stata.
The modal value of a vector if a unique mode exists, else output determined by method
Jared E. Knowles
table
which this function uses
a <- c(month.name, month.name)
statamode(a, method="stata") # returns "." to show no unique mode; useful for ddply
statamode(a ,method="sample") # randomly pick one
a <- c(LETTERS, "A" , "A")
statamode(a)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.