count_pattern: Count the total number, by column, of entries in a data frame...

count_patternR Documentation

Count the total number, by column, of entries in a data frame that match a string pattern

Description

Returns a named integer vector with elements that give the number of entries in the corresponding column of df that match the string pattern pattern. No coercion is used so only characters or factors are matched.

Usage

count_pattern(df, pattern, all = FALSE)

Arguments

df

A data frame.

pattern

Pattern to look for. Passed to stringr::str_detect().

all

By default variables with no matches are omitted from the output. Set all=T to show all.

Details

Note that repeated occurrences of pattern in a single string are only counted once (see examples).

Examples

x <- data.frame(a = c("an", "banana", "candy"), b = c("on", "bon", "bonbon"), d = 1:3)
count_pattern(x, "an", all = TRUE)
count_pattern(x, "an")
count_pattern(x, "b")
count_pattern(x, "1") # not matched to integers


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