[R] return NA

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Wed Jun 29 21:57:13 CEST 2005



dscully at fd9ns01.okladot.state.ok.us wrote:
> 
> 
> 
> A<-c(1,2,NA,7,5)
> B<-c(3,4,1,4,1)
> C<-c(6,5,6,NA,9)
> D<-c(8,7,4,6,2)
> df1<-cbind(A,B,C,D)
> for(i in seq(1,ncol(df1)-1, by=2)) {
>    ifelse(df1[,i]=="NA",df1[,i+1]=="NA",df1[,] )  }
> 
> 
> Tried several variations but none worked.  I wish to find any NA's in
> column's 1 or 3 and change the numerical value to the right  of  the "
> NA"'s .  In this case I wish to replace the 1 and 6 respectively with "NA".
> Any help would be appreciated.
> 

I think you want ?is.na.

for(i in seq(1, ncol(df1) - 1, by = 2))
   df1[, i + 1] <- ifelse(is.na(df1[, i]), df1[, i], df1[, i + 1])

--sundar




More information about the R-help mailing list