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
答案 0 :(得分:0)
尝试左加入df2
和df1
并将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"))