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

StellathePug ritacarreira at hotmail.com
Tue Aug 23 18:29:10 CEST 2011


Hello everyone,
I am trying to figure out a way of replacing missing observations in one of
the variables of a data frame by values of another variable. For example,
assume my data is X

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")

I want to change X1 so that instead of the missing values it uses the values
in X2 (regardless of whether these are missing). So my X1, should become
X$X1 <- c(9, 6, 1, 3, 9, "NA", 5, 4, 1, 3).

I have searched online for a while and looked at the manuals and the best
(unsuccessful) attempt I have come up with is

X$X1[X$X1=="NA"] <- X$X2

and that produces the following X1 

X$X1<-c(9, 6, 1, 3, 9, 6, "NA", 3, "NA", "NA")

and generates the following warning:

Warning messages:
1: In `[<-.factor`(`*tmp*`, X$X1 == "NA", value = c(5L, 3L, 2L, 6L,  :
  invalid factor level, NAs generated
2: In x[...] <- m :
  number of items to replace is not a multiple of replacement length

I think that my error is that it is ignoring the non-missing values of X1
and the dimensions don't match. But what I want my code to do is to look at
the rows of X1, see if it's a missing value; if it is, replace it with the
value that is in the row of X2; if it's not missing, leave it as is.

What am I doing wrong?

Thank you very much!
Rita


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



More information about the R-help mailing list