Hi all, I have a huge matrix of TRUE/FALSE table like following, and I want to count the number of TRUEs in each row. Instead of looping through each row and do length(Z[Z==TRUE]), I am wondering if there is an easier way of doing this. [,1] [,2] [,3] [1,]TRUE FALSE FALSE [2,]FALSE TRUE TRUE Thank you in advance. Wendy -- View this message in context: http://r.789695.n4.nabble.com/count-number-of-TRUEs-in-each-row-tp3440486p3440486.html Sent from the R help mailing list archive at Nabble.com.
Ms Qiao, On 10 April 2011 14:24, Wendy <wendy2.qiao@gmail.com> wrote:> I have a huge matrix of TRUE/FALSE table like following, and I want to > count > the number of TRUEs in each row. Instead of looping through each row and do > length(Z[Z==TRUE]), I am wondering if there is an easier way of doing this. > > [,1] [,2] [,3] > [1,]TRUE FALSE FALSE > [2,]FALSE TRUE TRUE >Does this help... Z[Z==T] ? -- Sent from my mobile device Envoyait de mon telephone mobil [[alternative HTML version deleted]]
On Sun, Apr 10, 2011 at 2:24 PM, Wendy <wendy2.qiao at gmail.com> wrote:> Hi all, > > I have a huge matrix of TRUE/FALSE table like following, and I want to count > the number of TRUEs in each row. Instead of looping through each row and do > length(Z[Z==TRUE]), I am wondering if there is an easier way of doing this. > > ? ? ? [,1] ? [,2] ? ?[,3] > [1,]TRUE FALSE FALSE > [2,]FALSE TRUE TRUE >rowSums(Z) Peter
Hi Wendy, try this: Z <- matrix(runif(1000)>0.5,10,100) rowSums(Z) HTH, Denes> Hi all, > > I have a huge matrix of TRUE/FALSE table like following, and I want to > count > the number of TRUEs in each row. Instead of looping through each row and > do > length(Z[Z==TRUE]), I am wondering if there is an easier way of doing > this. > > [,1] [,2] [,3] > [1,]TRUE FALSE FALSE > [2,]FALSE TRUE TRUE > > Thank you in advance. > Wendy > > -- > View this message in context: > http://r.789695.n4.nabble.com/count-number-of-TRUEs-in-each-row-tp3440486p3440486.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. >