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.
You need to use is.na(df1[,i]) to test for NA. Andy> From: dscully at fd9ns01.okladot.state.ok.us > > > > > > 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. > > ______________________________________________ > 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 > > >
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
Here is a way to do it without a loop that could save some time for a big dataset.> df1A B C D [1,] 1 3 6 8 [2,] 2 4 5 7 [3,] NA 1 6 4 [4,] 7 4 NA 6 [5,] 5 1 9 2> df2<-cbind(0,ifelse(is.na(df1),NA,0))[,-ncol(df1)-1] > df2A B C [1,] 0 0 0 0 [2,] 0 0 0 0 [3,] 0 NA 0 0 [4,] 0 0 0 NA [5,] 0 0 0 0> df3<-df1+df2 > df3A B C D [1,] 1 3 6 8 [2,] 2 4 5 7 [3,] NA NA 6 4 [4,] 7 4 NA NA [5,] 5 1 9 2 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of dscully at fd9ns01.okladot.state.ok.us Sent: June 29, 2005 3:50 PM To: r-help at stat.math.ethz.ch Subject: [R] return NA 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. ______________________________________________ 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