R/grep.R

Defines functions grep_not_or grep_not_and grep_and grep_or

Documented in grep_and grep_not_and grep_not_or grep_or

#' Grep for vectors
#'
#' @param x vectors with one or more characters
#' @param patterns vectors with one or more characters
#' @name grep
#' @return vectors
#' @export
#'
#' @examples
#' x=c('a12','a','b')
#' patterns=c('b','1')
#' grep_or(x,patterns)
grep_or <- function(x,patterns){
    lp=lapply(patterns, function(i) grepl(i,x))
    res=0
    for (i in lp) res=res+i
    x[res>0]
}
#' @rdname grep
#' @export
#'
#' @examples
#' x=c('a12','a1','b')
#' patterns=c('a','1')
#' grep_and(x,patterns)
grep_and <- function(x,patterns){
    lp=lapply(patterns, function(i) grepl(i,x))
    res=0
    for (i in lp) res=res+i
    x[res==length(patterns)]
}
#' @rdname grep
#' @export
#'
#' @examples
#' x=c('a12','a','b')
#' patterns=c('a','1')
#' grep_not_and(x,patterns)
grep_not_and <- function(x,patterns){
    lp=lapply(patterns, function(i) grepl(i,x))
    res=0
    for (i in lp) res=res+i
    x[res!=length(patterns)]
}
#' @rdname grep
#' @export
#'
#' @examples
#' x=c('a12','a','b')
#' patterns=c('a','1')
#' grep_not_or(x,patterns)
grep_not_or <- function(x,patterns){
    lp=lapply(patterns, function(i) grepl(i,x))
    res=0
    for (i in lp) res=res+i
    x[res==0]
}

Try the set package in your browser

Any scripts or data that you put into this service are public.

set documentation built on Feb. 26, 2021, 5:10 p.m.