Last active
December 22, 2015 05:08
-
-
Save christophergandrud/6421551 to your computer and use it in GitHub Desktop.
Subsets a data frame if a specified pattern is found in a character string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Subset a data frame if a specified pattern is found in a character string | |
#' | |
#' @param data data frame. | |
#' @param pattern character vector containing a regular expressions to be matched in the given character vector. | |
#' @param character vector of the variables that the pattern should be found in. | |
#' @param keep.found logical. whether or not to keep observations where the pattern is found (\code{TRUE}) or not found (\code{FALSE}). | |
#' @param useBytes logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See \code{\link{grep}}. | |
grepl.sub <- function(data, patterns, var, keep.found = TRUE, useBytes = TRUE){ | |
data$y <- grepl(pattern = paste0(patterns, collapse="|"), x = data[, var], | |
useBytes = useBytes) | |
subdata <- subset(data, y == keep.found) | |
subdata$y <- NULL | |
subdata | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment