counts returns the most or least common unique value(s) of a vector depending on whether ascending or descending sorting is used. Also useful for identifying data entry errors or rare cases. For complex use cases see describe.

counts(y, n = "all", order = c("d", "a", "i"), sep = "_", na.rm = TRUE)

Arguments

y

A vector/variable (required).

n

The number of unique values you want frequency counts for. Default is "all".

order

"d" for descending/decreasing order. "a" or "i" for ascending/increasing order.

sep

A character string to use to separate unique values from their counts ("_" by default).

na.rm

Should missing values be omitted (TRUE/FALSE)?

Value

A character vector of the unique value frequency counts from the input vector sorted in the chosen order. Return values are structured as "value_count", where the "_" portion takes on the value of the sep argument. Returning a character vector makes subsequent manipulation with stringr and other tidyverse tools fairly easily.

See also

Author

Craig P. Hutton, Craig.Hutton@gov.bc.ca

Examples

#using a numeric vector
y <- c(1, 1, 2, 3, 4, 5, 6, 2, 3, 4, 2, 9, 5, 6, 7, 23, 5, 6, 7, 8, 3, 4, 5, 6)

counts(y) #all unique values of y in order of descending frequency \code{y}
#>  [1] "5_4"  "6_4"  "2_3"  "3_3"  "4_3"  "1_2"  "7_2"  "23_1" "8_1"  "9_1" 
counts(y, n = 1) #the most common value of \code{y}
#> [1] "5_4"
counts(y, n = 5) #the top 5 most common unique values of \code{y}
#> [1] "5_4" "6_4" "2_3" "3_3" "4_3"