[R] NA's in if statement
Spencer Graves
spencer.graves at pdf.com
Fri Feb 11 23:59:01 CET 2005
The following provides a sample use if "is.na" in an context
similar to yours, that also uses vector arithmetic, so you don't need a
loop:
> tst <- (-2):2
> (tst2 <- 1+tst/(tst^2))
[1] 0.5 0.0 NaN 2.0 1.5
> (sel <- (tst2==0))
[1] FALSE TRUE NA FALSE FALSE
> sel[is.na(tst2)] <- FALSE
> tst2[sel] <- 123
> tst2
[1] 0.5 123.0 NaN 2.0 1.5
hope this helps. spencer graves
michael watson (IAH-C) wrote:
>Some careful use of is.na() may help.
>Either by using it to remove NA values or as part of the if statement.
>
>Or:
>
>my.wdir <- na.omit(dat$wdir)
>for(i in 1:length(my.wdir)) {
>
>#etc
>
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch on behalf of lindsayv at unbc.ca
> Sent: Fri 2/11/2005 9:34 PM
> To: r-help at stat.math.ethz.ch
> Cc:
> Subject: [R] NA's in if statement
>
>
>
> Hello,
>
> I am having trouble dealing with NA values in if statements such as:
> i<-1
> for(i in 1:length(dat$wdir)){
> if (dat$wspd[i]==0){
> dat$wdir[i]<-0
> }
> }
>
> I get the following error due to the presence of NS values in the data:
> Error in if (dat$wspd[i] == 0) { : missing value where TRUE/FALSE needed
>
> Sorry if this is an old question but I was unable to resolve this from the
> info on na.action etc. or old archives.
>
> Thank you for any suggestions,
>
> Vera Lindsay
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
More information about the R-help
mailing list