Skip to content
Prev 24561 / 398502 Next

Counting NA?

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
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:
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
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._