Skip to content
Prev 305233 / 398503 Next

Combine two variables

Hello,

Inline.

Em 11-09-2012 15:57, Simon Kiss escreveu:
No, not necessarily. You are using sample() and there's no reason to 
believe the sampled values for var1 and var2 are going to be different. 
My first try gave me several rows with both columns NA. Then I've used 
set.seed() and it became reproducible.

set.seed(1)
df1 <- data.frame(var1=sample(c('a','b','c',NA), replace=TRUE, size=100),
     var2=sample(c('a','b','c',NA), replace=TRUE, size=100))
sum(is.na(df1$var1) & is.na(df1$var2))  # 8

So I suppose this is not the case with your real dataset.
Try the following.

df1$var3 <- df1$var1
df1$var3[is.na(df1$var1)] <- df1$var2[is.na(df1$var1)]


Hope this helps,

Rui Barradas