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

David Huffer David.Huffer at csosa.gov
Tue Jul 7 21:10:50 CEST 2009


How about

countN <- function ( v ) {
  sum ( !is.na ( v ) ) - sum ( is.na ( v ) )
} 

--
 David
 
 -----------------------------------------------------
 David Huffer, Ph.D.               Senior Statistician
 CSOSA/Washington, DC           david.huffer at csosa.gov
 -----------------------------------------------------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Godmar Back
Sent: Tuesday, July 07, 2009 2:57 PM
To: R-help at r-project.org
Subject: [R] how to count number of elements in a vector that are not NA ?

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