How can I replace the \"NAs\" of a variable VAR1 with with the values of the second variable VAR2 to create a third variable VAR3 in R? The data looks like this:
<
One way is to use ifelse:
ifelse
DF <- transform(DF, VAR3 = ifelse(!is.na(VAR1), VAR1, VAR2))
where transform was used to avoid typing DF$ over and over, but maybe you will prefer:
transform
DF$
DF$VAR3 <- ifelse(!is.na(DF$VAR1), DF$VAR1, DF$VAR2)