Hi all,

This really annoyed since I thought this would be easy with some higher
order function.

Here is what I want:

I have a data frame with two columns, one is ID, another one is Name. I
want to get all rows whose name starts with some specific prefix. I thought
this should be a one-liner instead of a long loop.

My code (in an R console) is shown below, I thought we could use some
boolean function to filter a data frame, but it seems I was wrong.

> id = c(1,2)
> name = c("prefix1.suffix1", "prefix2.suffix2")
> data = data.frame(id, name)
> # Next, define a function which chooses a specific prefix:
> filtername = function(d) { unlist(strsplit(as.character(d$name),
"[.]"))[[1]] == "prefix2" }
> # This function seems work!
> filtername(data[1,])
[1] FALSE
> filtername(data[2,])
[1] TRUE
> # Wrong results.
> data[filtername(data),]
[1] id   name
<0 rows> (or 0-length row.names)

I would be appreciate if anyone could help.

Thank you!

-Monnand

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to