How to filter a data frame with user defined function?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to filter a data frame with user defined function?

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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: How to filter a data frame with user defined function?

Jim Lemon-4
Hi Monnand,
Is this what you are looking for?

data[grep("prefix1",data$name),]
data[grep("prefix2",data$name),]

Jim

On Wed, Jan 28, 2015 at 6:51 PM, Monnand <[hidden email]> wrote:

> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.