bruclee
2010-Oct-21 02:41 UTC
[R] Help: Using vectorization method for vectors comparision
I am trying to compare two sorted vectors, all elements in both vectors are not duplicated. Ex. a = c[5, 10, 13, 19, 23] b = c[1, 4, 7, 9, 15] For each element in a, i need find the max element in b which is smaller than it, so the short answer will look like [4, 9, 9, 15, 15]. I dont want to use any loop since my real project contains element in millions. Is there any way to speed up my operation? Many Thanks. -- View this message in context: http://r.789695.n4.nabble.com/Help-Using-vectorization-method-for-vectors-comparision-tp3004952p3004952.html Sent from the R help mailing list archive at Nabble.com.
Wu Gong
2010-Oct-21 04:29 UTC
[R] Help: Using vectorization method for vectors comparision
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.
bruclee
2010-Oct-21 05:07 UTC
[R] Help: Using vectorization method for vectors comparision
Very Nice! Thanks a lot! Btw, I think "match" function should also do the work for the last two steps. :) -- View this message in context: http://r.789695.n4.nabble.com/Help-Using-vectorization-method-for-vectors-comparision-tp3004952p3005032.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2010-Oct-21 12:28 UTC
[R] Help: Using vectorization method for vectors comparision
try this:> a <- c(5, 10, 13, 19, 23) > > b <- c(1, 4, 7, 9, 15) > # use outer for comparison > z <- outer(a, b, ">") > # use rowSums to get the indices (may have to check for zero) > b[rowSums(z)][1] 4 9 9 15 15>On Wed, Oct 20, 2010 at 10:41 PM, bruclee <brouce33 at gmail.com> wrote:> > I am trying to compare two sorted vectors, all elements in both vectors are > not duplicated. Ex. > a = c[5, 10, 13, 19, 23] > b = c[1, 4, 7, 9, 15] > For each element in a, i need find the max element in b which is smaller > than it, so the short answer will look like [4, 9, 9, 15, 15]. > I dont want to use any loop since my real project contains element in > millions. Is there any way to speed up my operation? Many Thanks. > -- > View this message in context: http://r.789695.n4.nabble.com/Help-Using-vectorization-method-for-vectors-comparision-tp3004952p3004952.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?