Learn R Programming

misty (version 0.6.7)

chr.grep: Multiple Pattern Matching

Description

This function searches for matches to the character vector specified in pattern within each element of the character vector x.

Usage

chr.grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
         fixed = FALSE, useBytes = FALSE, invert = FALSE, check = TRUE)

chr.grepl(pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE, useBytes = FALSE, check = TRUE)

Value

Returns a integer vector with the indices of the mathces when value = FALSE, character vector containing the matching elements when value = TRUE, or a logical vector when using the chr.grepl function.

Arguments

pattern

a character vector with character strings to be matched.

x

a character vector where matches are sought.

ignore.case

logical: if FALSE (default), the pattern matching is case sensitive and if TRUE, case is ignored during matching.

perl

logical: if TRUE Perl-compatible regexps are used.

value

logical: if FALSE (default), a vector containing the (integer) indices of the matches determined by grep is returned, and if TRUE, a vector containing the matching elements themselves is returned.

fixed

logical: if TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

useBytes

logical: if TRUE, the matching is done byte-by-byte rather than character-by-character. See 'Details'.

invert

logical: if TRUE, function returns indices or values for elements that do not match.

check

logical: if TRUE (default), argument specification is checked.

Author

Takuya Yanagida

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole

See Also

chr.gsub, chr.omit, chr.trim

Examples

Run this code
chr.vector <- c("James", "Mary", "Michael", "Patricia", "Robert", "Jennifer")

# Example 1: Indices of matching elements
chr.grep(c("am", "er"), chr.vector)

# Example 2: Values of matching elements
chr.grep(c("am", "er"), chr.vector, value = TRUE)

# Example 3: Matching element?
chr.grepl(c("am", "er"), chr.vector)

Run the code above in your browser using DataLab