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
解决方案
尝试左连接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"))
推荐阅读
- python - 如何将每个十进制列表元素从二进制转换(优化方式)?
- python - DAG 中最长的总路径
- c# - 关闭的 XML:文件无法在受保护的视图中打开,打开导出的 excel 时出错
- json - 将两个 JSON 查询串在一起并选择“和”单选按钮
- php - 需要帮助将返回的 PHP 数据分配给 Angular 2 对象数组
- c# - 保存具有关系 ID 的一对多实体
- java - 我如何在杰克逊中定义一个模式来反序列化它
- bazel - libtls/gnutls 的 Bazel 构建?
- linux - linux cli find 命令但忽略某个目录
- python - 如何在数据类中使用 field()?