[R] Count number of zeros in a collumn

William Dunlap wdunlap at tibco.com
Mon Sep 28 01:52:46 CEST 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Murat Tasan
> Sent: Sunday, September 27, 2009 4:27 PM
> To: r-help at r-project.org
> Subject: Re: [R] Count number of zeros in a collumn
> 
> more generally, if you want to test for some minimum threshold T on
> the number of zeros, try:
> 
> > if(length(which(dart[,1977] == 0)) < T) { # some code to 
> handle the too-few-zeros-case }

Note that
   sum( dart[,1977]==0 )
gives the same result as
   length(which( dart[,1977]==0 ))
with less typing and using less memory and time.
Arithmetic on logicals treats TRUE as 1 and FALSE
as 0.

(sum() forces you to decide what to do with NA's
in the data; which() always discards them.)

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com  

> 
> 
> 
> 
> On Sep 27, 4:54 pm, Marcio Resende <mresende... at yahoo.com.br> wrote:
> > I have a matrix 700x2000 which is sampled in each cycle 
> from another matrix
> > 788x2000 with the numbers 0,1 and 9
> >
> > There is one specific collumn of this matrix, dart[,1977], 
> that usually,
> > after the samplimg procedure has only 1 and 9 (because the 
> zero frequency in
> > this collumn is low).
> > However, when this happens, I want to include an IF 
> conditional in my code.
> > so basically what i wanted to do was to count the number of 
> zeros in this
> > collumn to use this information in my conditional:
> >
> > something like:
> >
> > if (the number of zeros in collumn [,1977] is zero) ... else ...
> >
> > I tried to find out indirectly by the rowsum but because of 
> the sampling
> > procedure the rowsum is not always the same
> >
> > Thank you very much
> > --
> > View this message in 
> context:http://www.nabble.com/Count-number-of-zeros-in-a-collu
> mn-tp25637516p2...
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > R-h... at r-project.org mailing 
> listhttps://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting 
> guidehttp://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> 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.
> 




More information about the R-help mailing list