Hi Wendy, Most of the binary operators can deal with matrices and vectors natively:
A<-c(12,3,4) B<-matrix(c(4,10,4,13,2,8),3,2) B [,1] [,2] [1,] 4 13 [2,] 10 2 [3,] 4 8 B<A [,1] [,2] [1,] TRUE FALSE [2,] FALSE TRUE [3,] FALSE FALSE Cheers, Tsjerk On Sun, Oct 30, 2011 at 8:55 AM, Jim Lemon <j...@bitwrit.com.au> wrote: > On 10/30/2011 02:51 PM, Wendy wrote: >> >> Hi, >> >> I have a vector and a matrix. For example, >> >> A = [ >> 12 >> 3 >> 4]; >> >> B = [ >> 4 13 >> 10 2 >> 4 8]; >> >> I am comparing A to each column of B using A>B[,ii], so the expected >> result >> is >> >> C = [ >> 1 0 >> 0 1 >> 0 0]; >> >> I am looking for a way to do this quickly instead of going through the for >> loop, but haven't had any luck yet? Any advice is appreciated. > > Hi Wendy, > You probably mean something like this: > > apply(B,2,`<`,A) > > which means roughly > "To each column of B, apply the function `<` using A as the comparison > values" > > You will get a matrix of TRUE/FALSE values that are pretty much equivalent > to your 0/1 values. Note that there are quite a few '*apply' functions and > 'apply' is only guaranteed to work on arrays and matrices. > > Jim > > ______________________________________________ > R-help@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. > -- Tsjerk A. Wassenaar, Ph.D. post-doctoral researcher Molecular Dynamics Group * Groningen Institute for Biomolecular Research and Biotechnology * Zernike Institute for Advanced Materials University of Groningen The Netherlands ______________________________________________ R-help@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.