如何在 R 中对两个数据框执行内连接和外连接?
一个内连接仅返回左表在右表中有匹配键的行,而一个外连接返回来自两个表中所有行。内连接记录在右表中有匹配键的左表记录。这可以通过使用合并函数来完成。
示例
内连接
> 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
内连接
> 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
广告