Left 與 Right 連接

R 的 data.table 資料合併

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

Left join

將右側 data.table 的資訊加入左側 data.table

merge(x = demographics, y = shipping, by = "name", all.x = TRUE)

R 的 data.table 資料合併

Right join

將左側 data.table 的資訊加入右側 data.table

merge(x = demographics, y = shipping, by = "name", all.y = TRUE)

R 的 data.table 資料合併

Right 與 Left 等價關係

# Right join
merge(x = demographics, y = shipping, by = "name", all.y = TRUE)

# Same as
merge(x = shipping, y = demographics, by = "name", all.x = TRUE)
R 的 data.table 資料合併

預設值

  • merge() 中,allall.xall.y 的預設值都是 FALSE

  • 使用 help("merge") 查詢函式參數的預設值

R 的 data.table 資料合併

練習說明

shippingdemographics 做 Left join

merge(demographics, shipping, by = "name", all.x = TRUE)

shippingdemographics 做 Right join

merge(demographics, shipping, by = "name", all.y = TRUE)
R 的 data.table 資料合併

一起來練習吧!

R 的 data.table 資料合併

Preparing Video For Download...