[R] RE: set functions
Jens Oehlschlägel-Akiyoshi
jens.oehlschlaegel-akiyoshi at mdfactory.de
Wed Jan 5 17:02:14 CET 2000
two more:
setequal3 and setequal4 handle NAs, setequal4 (currently) is fastest
"setequal" <- function(x,y)
length(x<-unique(x))==length(y<-unique(y)) && all(sort(x)==sort(y))
"setequal2" <- function(x, y) all(c(match(x, y, 0)>0, match(y, x, 0)>0))
"setequal3" <- function(x,y)
length(x<-as.character(unique(x)))==length(y<-as.character(unique(y))) &&
all(sort(x, na.last=TRUE)==sort(y, na.last=TRUE))
"setequal4" <- function(x,y){
x <- unique(x)
y <- unique(y)
if (length(x)!=length(y)) return(FALSE)
if (any(is.na(x))!=any(is.na(y))) return(FALSE)
all(sort(x[!is.na(x)])==sort(y[!is.na(y)]))
}
setequal(c(NA, 1:4), c(1:4, NA)) # TRUE
setequal2(c(NA, 1:4), c(1:4, NA)) # TRUE
setequal3(c(NA, 1:4), c(1:4, NA)) # TRUE
setequal4(c(NA, 1:4), c(1:4, NA)) # TRUE
setequal(c(NA, 1:4), c(1:4, 5)) # FALSE plus warning message
setequal2(c(NA, 1:4), c(1:4, 5)) # FALSE
setequal3(c(NA, 1:4), c(1:4, 5)) # FALSE
setequal4(c(NA, 1:4), c(1:4, 5)) # FALSE
x<-1:50000
y<-x[order(runif(50000))]
system.time(setequal(x,y))
system.time(setequal2(x,y))
system.time(setequal3(x,y))
system.time(setequal4(x,y))
x <- rep(1:50, 1000)
y<-x[order(runif(50000))]
system.time(setequal(x,y))
system.time(setequal2(x,y))
system.time(setequal3(x,y))
system.time(setequal4(x,y))
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list