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?
Thanks!
--
Ubuntu Linux 6.06 (Dapper Drake)
[[alternative HTML version deleted]]
> av <- c("UTI","ABC","UIT","UTI","UTI") > grep("UTI", av)[1] 1 4 5> # if you really want TRUE/FALSE > occurs <- logical(length(av)) > occurs[grep("UTI", av)] <- TRUE > occurs[1] TRUE FALSE FALSE TRUE TRUE> >On 12/9/06, Neil McLeod <neil.a.mcleod@gmail.com> 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? > > Thanks! > -- > Ubuntu Linux 6.06 (Dapper Drake) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
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