AW: [Rd] Proposal: Generalizing unique() and duplicated()

Kaspar Pflugshaupt pflugshaupt@geobot.umnw.ethz.ch
Tue, 6 Feb 2001 13:34:01 +0100


On Tuesday 06 February 2001 12:36, Dr. Jens Oehlschlägel wrote:
> I like the idea. Why don't you call duplicated.matrix() directly in
> unique.matrix() and duplicated.data.frame() in unique.data.frame() ?
>
> Jens Oehlschlägel

Good point. I guess I got carried away with using methods (having just gotten 
the hang of the concept). :-)

Anyway, here's a corrected version:

----------------------------------------------------

"unique.default" <- get("unique", pos="package:base")    # old version becomes
                                                         # default behaviour
"unique" <- function(object, ...)
{
   if (data.class(object)=="matrix")
       return(unique.matrix(object, ...))
   else
       UseMethod("unique")      # doesn't seem to work for matrices, hence 
}                               # the condition
                         


"duplicated.default" <- get("duplicated", pos="package:base")    

"duplicated" <- function(object, ...)
{
   if (data.class(object)=="matrix")
       return(duplicated.matrix(object, ...))
   else
       UseMethod("duplicated")  
}


"duplicated.matrix" <-
  function(mat, MARGIN=1)    # defaulting to work on rows
{
  strvect <- drop(apply(mat, MARGIN, function(x) paste(x, collapse = "\r")))
  return(duplicated(strvect))
}


"unique.matrix" <-
  function(mat, MARGIN=1)    # defaulting to work on rows
{
  dup <- duplicated.matrix(mat, MARGIN)
  return(if (MARGIN==1) mat[!dup,] else mat[,!dup])
}


"duplicated.data.frame" <-
  function(df, MARGIN=1)
{
  strvect <- drop(apply(as.matrix(df), MARGIN, function(x) paste(x, collapse 
= "\r")))
  duplicated(strvect)
}


"unique.data.frame" <-
  function(df, MARGIN=1)
{
  dup <- duplicated.data.frame(df, MARGIN)
  return(if (MARGIN==1) df[!dup,] else df[,!dup])
}

----------------------------------------------------

Cheers

Kaspar Pflugshaupt
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._