get_mode | R Documentation |
Determines which element of list appears most frequently. Based on
base::which.max()
, so if multiple values appear with the same
frequency will return the first one. Ignores NA
values. In the
context of swimming data is often used to clean team names, as in the Lilly
King example below.
get_mode(x, type = "first")
x |
A list. |
type |
a character string of either |
the element of x
which appears most frequently. Ties go to
the lowest index, so the element which appears first.
a <- c("a", "a", "b", "c")
get_mode(a)
ab <- c("a", "a", "b", "b", "c") # returns "a", not "b"
get_mode(ab)
#' ab <- c("a", "a", "b", "b", "c") # returns "a" and "b"
get_mode(ab, type = "all")
a_na <- c("a", "a", NA, NA, "c")
get_mode(a_na)
numbs <- c(1, 1, 1, 2, 2, 2, 3, NA)
get_mode(numbs, type = "all")
Name <- c(rep("Lilly King", 5))
Team <- c(rep("IU", 2), "Indiana", "IUWSD", "Indiana University")
df <- data.frame(Name, Team, stringsAsFactors = FALSE)
df$Team <- get_mode(df$Team)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.