From pflugshaupt@geobot.umnw.ethz.ch  Tue Feb  6 12:34:01 2001
From: pflugshaupt@geobot.umnw.ethz.ch (Kaspar Pflugshaupt)
Date: Tue, 6 Feb 2001 13:34:01 +0100
Subject: AW: [Rd] Proposal: Generalizing unique() and duplicated()
In-Reply-To: <01C09039.6E17C100.jens.oehlschlaegel@bbdo-interone.de>
References: <01C09039.6E17C100.jens.oehlschlaegel@bbdo-interone.de>
Message-ID: <01020613340100.00877@Aconitum>

On Tuesday 06 February 2001 12:36, Dr. Jens Oehlschlgel 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 Oehlschlgel

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


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