set functions

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
04 Jan 2000 17:55:02 +0100


Jonathan Rougier <J.C.Rougier@durham.ac.uk> writes:

> > equiv<-function(x,y) 
> >     length(x<-unique(x))==length(y<-unique(y)) && 
> >     all(sort(x)==sort(y)) 
> 
> Yes, I wondered about that, and also about
> 
> "equiv" <-
> function(x, y) {
>   x <- unique(x)
>   y <- unique(y)
>   length(x)==length(y) && all(1:length(y) == sort(match(x, y, 0)))
> }
> 
> but I thought that perhaps a sort would be more expensive than a second
> call to match, and more so for two sorts.  Cheers, Jonathan.

Watch:

> x<-1:50000
> y<-x[order(runif(50000))]
> "equiv2" <- function(x, y) all(c(match(x, y, 0)>0, match(y, x, 0)>0))
> equiv<-function(x,y) 
+     length(x<-unique(x))==length(y<-unique(y)) && 
+     all(sort(x)==sort(y)) 
> system.time(equiv2(x,y))
[1] 3.10 0.02 3.00 0.00 0.00
> system.time(equiv(x,y))
[1] 0.77 0.00 1.00 0.00 0.00
> 


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._