[R] Help: Using vectorization method for vectors comparision

Wu Gong wg2f at mtmail.mtsu.edu
Thu Oct 21 06:29:44 CEST 2010


Hi Bruclee,

?rle may help.

a <- c(5, 10, 13, 19, 23)
b <- c(1, 4, 7, 9, 15) 
ab <- data.frame(value = c(a,b), type=c(rep(0,length(a)),rep(1,length(b))))
ab <- ab[order(ab$value),]
ab$v2 <- cumsum(ab$type)
ab$matched <- rep(ab$value[ab$type==1],rle(ab$v2)$lengths)
(result <- ab[ab$type==0,c("value","matched")])

The code should work. 

Regards.

-----
A R learner.
-- 
View this message in context: http://r.789695.n4.nabble.com/Help-Using-vectorization-method-for-vectors-comparision-tp3004952p3005012.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list