[R] How to filter a data frame with user defined function?

Monnand monnand at gmail.com
Wed Jan 28 08:51:54 CET 2015


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]]



More information about the R-help mailing list