如何在 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
廣告