[R] Testing if all elements are equal in a vector/matrix

baptiste auguie baptiste.auguie at gmail.com
Tue Jun 16 15:20:16 CEST 2009


[correcting a stupid error in my previous post]


testTwoStages <- function(x, y, head.stop = 100){

if(!isTRUE(all(head(x, head.stop) == head(y, head.stop))))
{
    print(paste("quick test returned FALSE"))
    return(FALSE)
} else {
    full.test = isTRUE(all(tail(x, length(x) - head.stop) == tail(y, 
length(y) - head.stop)))
    print(paste("full test returned",  full.test))
   
    return(full.test)
}

}


x <- seq(1, 1e6)
y <- x
y[325] <- 0


system.time(testTwoStages(x, y)) # FALSE and slow
system.time(testTwoStages(x, y, 400)) # FALSE and quick
system.time(testTwoStages(x, x)) # TRUE and slow



-- 
_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list