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]]
Jim Lemon
2015-Jan-28 08:04 UTC
[R] How to filter a data frame with user defined function?
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 <monnand at gmail.com> 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]] > > ______________________________________________ > R-help at 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.