如何使用匹配的行替换NA
中three
列中的A
值' three
中的B
列中的值?
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
答案 0 :(得分: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"