charCount: Count the Occurrences of a Pattern in Text

View source: R/utils.R

charCountR Documentation

Count the Occurrences of a Pattern in Text

Description

Counts the number of times a specified pattern appears in a given text string or vector of strings. If the pattern is a single character or a sequence of characters, the function returns the number of occurrences of the pattern in "stosearch".

Usage

charCount(pattern, stosearch, ignore.case = FALSE)

Arguments

pattern

Character. The pattern to search for within the text string(s).

stosearch

Character. The text string(s) in which to search for the pattern.

ignore.case

Logical. If TRUE, the search is case-insensitive. Default is FALSE.

Details

When the pattern is a regular expression, the function returns the number of times the expression matches in "stosearch". For specific regex patterns like ".", you should escape it: e.g., charCount("\.txt", c("test.txt", "sample.txt")).

Value

Integer. The total count of occurrences of the pattern in the text string(s).

See Also

nchar

Examples

charCount("a", "banana")  # Returns 3 (three 'a's in "banana")
charCount("a", c("banana", "apple"))  # Returns 4 
charCount("\\d", "13ab2c")  # Returns 3 (three digits in "13ab2c")
charCount("\\.txt", c("test.txt", "sample.txt"))


Epiconcept-Paris/STRAP-epiuf documentation built on Aug. 5, 2024, 3:41 a.m.