get_mode {SwimmeR} | R Documentation |
Find the mode (most commonly occurring) element of a list
Description
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.
Usage
get_mode(x, type = "first")
Arguments
x |
A list. |
type |
a character string of either |
Value
the element of x
which appears most frequently. Ties go to
the lowest index, so the element which appears first.
Examples
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)
[Package SwimmeR version 0.14.2 Index]