2017-01-09 102 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

相关问题