Learn R Programming

DataCombine (version 0.1.13)

grepl.sub: Subset a data frame if a specified pattern is found in a character string

Description

Subset a data frame if a specified pattern is found in a character string

Usage

grepl.sub(data, patterns, Var, keep.found = TRUE, useBytes = TRUE)

Arguments

data
data frame.
patterns
character vector containing a regular expressions to be matched in the given character vector.
Var
character vector of the variables that the pattern should be found in.
keep.found
logical. whether or not to keep observations where the pattern is found (TRUE) or not found (FALSE).
useBytes
logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See grep.

Examples

Run this code
# Create data frame
ABData <- data.frame(a = c("London, UK", "Oxford, UK", "Berlin, DE", "Hamburg, DE", "Oslo, NO"),
                     b = c(8, 0.1, 3, 2, 1))

# Keep only data from Germany (DE)
ABGermany <- grepl.sub(data = ABData, patterns = "DE", Var = "a")

Run the code above in your browser using DataLab