首页 > 解决方案 > 如何行绑定第二个表中第一个表列中的不匹配数据

问题描述

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

标签: r

解决方案


尝试左连接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"))

推荐阅读