2017-01-09 13 views
0

我怎樣才能在B替換列threeNAA與匹配行值從three列?匹配一個數據幀的多個列和替換值

A 
    one     two three 
1 A ram 1500 pickup 4wd  0 
2 B    jetta  0 
3 C   4runner 4wd <NA> 
4 D  mountaineer 4wd  5 
5 E   range rover <NA> 
6 F ram 1500 pickup 4wd  0 
7 G    jetta  0 
8 H   4runner 4wd <NA> 
9 I  mountaineer 4wd  5 
10 J   range rover  0 
11 K ram 1500 pickup 4wd  0 
12 L    jetta <NA> 
13 M   4runner 4wd  0 
14 N  mountaineer 4wd  0 
15 O   range rover <NA> 
16 P ram 1500 pickup 4wd  0 
17 Q    jetta  5 
18 R   4runner 4wd  5 
19 S  mountaineer 4wd <NA> 
20 T   range rover <NA> 



B 
    one    two three 
3 C  4runner 4wd  1 
5 E  range rover  1 
8 H  4runner 4wd  1 
12 L   jetta  1 
15 O  range rover  1 
19 S mountaineer 4wd  1 
20 T  range rover  1 

回答

2

假設你的兩個表是數據幀:

C <- merge(A, B, by = c("one", "two"), all.x = TRUE) 
C$three.x <- ifelse(is.na(C$three.x), C$three.y, C$three.x) 
C$three.y <- NULL 
names(C)[3] <- "three" 
+0

如果試圖使用ifelss(),現在我的想法是,_both是和否的說法應該來自同一object_ ,讓我嘗試一下。謝謝 – Bhail

相關問題