[R] Filtering a data frame by regular expression

Chuck Cleland ccleland at optonline.net
Sat Dec 9 22:44:19 CET 2006


Neil McLeod wrote:
> Hello,
> 
> I am having difficulty filtering a data frame.
> 
> I would like to take all the rows of a data frame where column A contains
> the regular expression "UTI" (or some other regex). Here's what I've got:
> 
> utiRE <- function (avector) {
> occurs <- c()
> r1 <- "UTI"
> for (x in avector) {
> if (!is.na(grep(r1,x,perl=TRUE))) {
> occurs <- c(occurs, TRUE)
> } else {
> occurs <- c(occurs, FALSE)
> }
> 
> 
> I know this is a clunky way of doing it, but I tried more natural ways first
> (i.e. without iteration), to no avail. I think the problem is that when I
> iterate through the list, the strings in avector get turned into numbers.
> 
> Any solutions?

  The following approach works for me:

iris[grep("a$", iris$Species),]

iris[grep("^v", as.character(iris$Species)),]

> Thanks!

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list