counts | R Documentation |
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)
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)? |
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.
Craig P. Hutton, Craig.Hutton@gov.bc.ca
table
, sort
#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} counts(y, n = 1) #the most common value of \code{y} counts(y, n = 5) #the top 5 most common unique values of \code{y}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.