count_matches: Count elements in data frame which exactly match a value

View source: R/count_matches.R

count_matchesR Documentation

Count elements in data frame which exactly match a value

Description

Counts the number of entries of each column in a data frame that exactly match the supplied values. No coercion is used so the type must also match (unless the column is a factor in which case character values will be matched to levels).

count_matches2() is a deprecated function that has been replaced by the detail argument.

Usage

count_matches(
  df,
  values = string_missing(),
  all = FALSE,
  prop = FALSE,
  detail = FALSE
)

count_matches2(df, values = string_missing(), all = FALSE, prop = FALSE)

Arguments

df

A data frame.

values

Vector of values to match.

all

By default, columns or values with no matches are omitted from the output. Set all=TRUE to show all.

prop

Default is to return counts of matches. Set prop=TRUE to return proportions of the number of rows.

detail

Logical. If TRUE returns a tibble with counts for each element in values. If FALSE then a named vector of total matches per column is returned.

Value

A named integer vector or tibble depending on the detail argument.

Examples

x <- data.frame(a = c("an", "banana", "candy"), b = c("on", "bon", "bonbon"), c = 1:3)
count_matches(x, "an", all = TRUE)
count_matches(x, "an")
count_matches(x, 1L)
count_matches(x, 1) # type must match
count_matches(x, "1") # type must match

df <- data.frame(x1 = c("a", ".", ".", "a"),
                 x2 = c("-", "-", "b", "b"),
                 x3 = rep("z", 4),
                 x4 = c("n/a", "f", "f", ""))
strs <- c(".", "-", "n/a", "na", "")
count_matches(df, strs, all = TRUE)
count_matches(df, strs, all = TRUE, detail = TRUE)
count_matches(df, strs, detail = TRUE)

jedwards24/edwards documentation built on Sept. 2, 2023, 8:16 a.m.