[R] Replacing NAs in one variable with values of another variable

StellathePug ritacarreira at hotmail.com
Wed Aug 24 15:35:38 CEST 2011


Thank you Dan and Ista!

Both of you are correct, I should have used NA rather than "NA" in my
example. So the correct code should be:

X <-as.data.frame(matrix(c(9, 6, 1, 3, 9, NA, NA,NA,NA,NA,
                           6, 4, 3,NA, NA, NA, 5, 4, 1, 3), ncol=2))
names(X)<-c("X1","X2")      

X$X1[is.na(X$X1)] <- X$X2[is.na(X$X1)] 

Where the last line replaces the missing observations of X1 by those of X2.
The "if else" statement also works.

Thank you very much, again!
Rita

--
View this message in context: http://r.789695.n4.nabble.com/Replacing-NAs-in-one-variable-with-values-of-another-variable-tp3763269p3765317.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list