如何在 R 中对两个数据框进行内连接和外连接?

r programmingserver side programmingprogramming

内连接仅返回左表中在右表中具有匹配键的行,而外连接返回两个表中的所有行,连接左表中在右表中具有匹配键的记录。这可以通过使用合并函数来完成。

示例

内连接

> df1 = data.frame(CustomerId = c(1:5), Product = c(rep("Biscuit", 3), rep("Cream", 2)))
> df1
  CustomerId Product
1 1 Biscuit
2 2 Biscuit
3 3 Biscuit
4 4 Cream
5 5 Cream
> df2 = data.frame(CustomerId = c(2, 5, 6), City = c(rep("Chicago", 2),
rep("NewYorkCity", 1)))
> df2
CustomerId City
1 2 Chicago
2 5 Chicago
3 6 NewYorkCity

Inner Join

> merge(x = df1, y = df2)
  CustomerId Product City
1 2 Biscuit Chicago
2 5 Cream Chicago

外连接

> merge(x = df1, y = df2, by = "CustomerId", all = TRUE)
  CustomerId Product City
1 1 Biscuit <NA>
2 2 Biscuit Chicago
3 3 Biscuit <NA>
4 4 Cream <NA>
5 5 Cream Chicago
6 6 <NA> NewYorkCity

相关文章