r - grep using a character vector with multiple patterns -
i trying use grep test whether vector of strings present in vector or not, , output values present (the matching patterns).
i have data frame this:
firstname letter alex a1 alex a6 alex a7 bob a1 chris a9 chris a6 i have vector of strings patterns found in "letter" columns, example: c("a1", "a9", "a6").
i check whether of strings in pattern vector present in "letter" column. if are, output of unique values.
the problem is, don't know how use grep multiple patterns. tried:
matches <- unique ( grep("a1| a9 | a6", myfile$letter, value=true, fixed=true) ) but gives me 0 matches not true, suggestions?
in addition @marek's comment not including fixed==true, need not have spaces in regular expression. should "a1|a9|a6".
you mention there lots of patterns. assuming in vector
tomatch <- c("a1", "a9", "a6") then can create regular expression directly this.
matches <- unique (grep(paste(tomatch,collapse="|"), myfile$letter, value=true))
Comments
Post a Comment