set functions

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


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

> On 4 Jan 2000, Peter Dalgaard BSA wrote:
> 
> > > "equiv" <- function(x, y) all(c(match(x, y, 0)>0, match(y, x, 0)>0))
> 
> > length(setdiff(x,y))==0 appears to be about twice as fast....
> 
> But I don't think that would be right!
> 
> length(setdiff(1:4, 1:5))==0	# is TRUE
> equiv(1:4, 1:5)			# clearly FALSE

Argh. I was thinking of the symmetric set difference. So you'd need 
setdiff(y,x)==0 & setdiff(x,y)==0 which is obviously only half as fast
as twice as fast....

However:

equiv<-function(x,y) 
    length(x<-unique(x))==length(y<-unique(y)) && 
    all(sort(x)==sort(y)) 

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