如何行绑定第二张表中第一张表的列中的不匹配数据

时间:2019-08-31 05:17:33

标签: r

How can I row bind the not match data in the column of first table from the second table......


library(gtools)
df1 <- data.frame(a = c("a", "b", "c"), number=c(4,3,2))
df2 <- data.frame(a = c("a", "b", "c", "k", "z"))


# fill in non-overlapping columns with NAs
df2[setdiff(names(df1), names(df2))] <- 0

rbind(df1, df2)

这是我的代码中的输出

a   number
1 a      4
2 b      3
3 c      2
4 a      0
5 b      0
6 c      0
7 k      0
8 z      0

我想要的输出..它只会在第一个表的行中添加不匹配的数据。

  a    number
1 a      4
2 b      3
3 c      2
4 k      0
5 z      0

1 个答案:

答案 0 :(得分:0)

尝试左加入df2df1并将NA替换为0。

df3 <- merge(df2, df1, all.x = TRUE)
df3$number[is.na(df3$number)] <- 0
df3

#  a number
#1 a      4
#2 b      3
#3 c      2
#4 k      0
#5 z      0

使用dplyr,您可以通过

执行相同的操作
library(dplyr)
df2 %>%
  left_join(df1, by = "a") %>%
  mutate(number = replace(number, is.na(number), 0))

或使用match

的另一个选项
df3 <- df2
df3$number <- df1$number[match(df2$a, df1$a)]
df3$number[is.na(df3$number)] <- 0

数据

df1 <- data.frame(a = c("a", "b", "c"), number=c(4,3,2))
df2 <- data.frame(a = c("a", "b", "c", "k", "z"))