[R] how to count number of elements in a vector that are not NA ?

Zhiliang Ma zhiliang.ma at gmail.com
Tue Jul 7 21:05:19 CEST 2009


how about sum(!is.na(x)) ?

On Tue, Jul 7, 2009 at 2:56 PM, Godmar Back<godmar at gmail.com> wrote:
> Hi,
>
> is there a simpler way to count the number of elements in a vector
> that are not NA than this:
>
> countN <- function (v) {
>    return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1)))
> }
>
> ?
>
>  - Godmar
>
> ______________________________________________
> 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