Skip to content

Instantly share code, notes, and snippets.

@christophergandrud
Last active December 22, 2015 05:08
Show Gist options
  • Save christophergandrud/6421551 to your computer and use it in GitHub Desktop.
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.
#' 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