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