[R] Length of vector without NA's

Joshua Wiley jwiley.psych at gmail.com
Thu Sep 23 17:23:00 CEST 2010


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/



More information about the R-help mailing list