[R] Equivalent of match for data.frame

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Jan 3 15:44:43 CET 2009


On Sat, 3 Jan 2009, Carlos J. Gil Bellosta wrote:

> Why not something like
>
> lapply(mydf, function(x) match(myarg, x) )

I don't think that is what is intended (it does not give the quoted result 
in 'S'), but I don't actually know (and we were not told what 'S' this 
is).  My guess is that the whole row has to match element by element. 
For that I would hash rows as in duplicated.data.frame.  Something like

match.data.frame <- function(x, table, ...)
     match(do.call("paste", c(x, sep = "\r")),
           do.call("paste", c(table, sep = "\r")), ...)

maybe with some added as.data.frame() calls for safety.

>
> ?
>
> Best regards,
>
> Carlos J. Gil Bellosta
> http://www.datanalytics.com
>
>
> On Sat, 2009-01-03 at 07:24 -0500, Sébastien wrote:
>> Dear R-users,
>>
>> I am translating a S script into R and having some troubles with the
>> match function. This function appears to work with vector and data.frame
>> in S, but not in R, e.g.:
>> a <- rep((1:4), each = 10)
>> b <- rep((1:10), times = 4)
>> mydf <- data.frame(a,b)
>> myarg <- mydf[1,]
>> match(myarg, mydf)
>>
>> # S returns 1 but R returns NA NA
>>
>> I guess one could use match(interaction(myarg), interaction(mydf)) to do
>> the job but I was just wondering if there was a more direct function.
>>
>> Thanks,
>>
>> Sebastien
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>
> ______________________________________________
> R-help at r-project.org 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list