# [R] Counting NA?

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Thu Oct 24 01:56:44 CEST 2002

```
> -----Original Message-----
> From: Matej Cepl [mailto:matej at ceplovi.cz]
> Sent: Wednesday, October 23, 2002 10:53 AM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Counting NA?
>
>
> On Wed, Oct 23, 2002 at 01:54:50PM +0200, David Wartel wrote:
> > Surely not the best method:
> > length(which(is.na(data\$S2)==TRUE))
>

For pure convenience, the gregmisc package includes a generic function
'nobs' which (for vectors) does sum(!is.na(x)).

> library(gregmisc)
> nobs( c(1,2,3,4,5,NA,NA,NA ))
[1] 5

> One this I was really surprised what that in order to get data
> from the same data I have to use following construct constantly:
>
> 	sumyes <- length(data\$S2[data\$S2 == 1 & !is.na(data\$S2)])
>
> Isn't there any way how to say R, that I want eliminate NA data
> for all my following calculations?

Well, the simplest thing would be to create a new variable with the missing
values removed,

y <- na.omit(data\$S2)

which is pretty much equivalent to

y <- data\$S2[!is.na(data\$S2)]

in this case.

For data frames it na.omit removes rows with any missing value, which is
what we often desire:

> data <- data.frame( x=rnorm(10), y=rnorm(10))
> data\$y[3] <- NA
> data
x          y
1  -1.40045667  0.1370657
2   0.16997476  0.4742317
3   0.66760007         NA
4   1.55857918  0.7787977
5  -0.70213268 -0.3806202
6  -1.03643219  0.4481208
7  -0.72489209  0.5521957
8  -0.86034676  1.3522844
9   0.50605859  1.5335474
10 -0.05295296  1.4871456
> na.omit(data)
x          y
1  -1.40045667  0.1370657
2   0.16997476  0.4742317
4   1.55857918  0.7787977
5  -0.70213268 -0.3806202
6  -1.03643219  0.4481208
7  -0.72489209  0.5521957
8  -0.86034676  1.3522844
9   0.50605859  1.5335474
10 -0.05295296  1.4871456
>

-Greg

LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```