Hi, this following code: x<-c(1,2,NA) length(x) returns 3, correctly counting numbers as well as NA's. How can I exclude NA's from this count? Ralf
try this sum(!is.na(x)) I hope it helps. Best, Dimitris On 9/23/2010 5:08 PM, Ralf B wrote:> Hi, > > this following code: > > x<-c(1,2,NA) > length(x) > > returns 3, correctly counting numbers as well as NA's. How can I > exclude NA's from this count? > > Ralf > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
> this following code: > > x<-c(1,2,NA) > length(x) > > returns 3, correctly counting numbers as well as NA's. How can I > exclude NA's from this count? >sum(!is.na(x)) Peter
Hi Ralf, The usual way (as others have shown you), takes advantage of the fact that the logical values TRUE and FALSE are counted as 1 and 0, respectively. is.na() returns TRUE if the value is NA, so to find how many are not NA, the result is reversed using ' ! '. Similar logic can be used to find how many meet any logical condition (e.g., sum(1:10 < 5) ). Cheers, Josh On Thu, Sep 23, 2010 at 8:08 AM, Ralf B <ralf.bierig at gmail.com> wrote:> Hi, > > this following code: > > x<-c(1,2,NA) > length(x) > > returns 3, correctly counting numbers as well as NA's. How can I > exclude NA's from this count? > > Ralf > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/