merge 函式

R 的 data.table 資料合併

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

Joins(連接)

  • 連接(join)概念來自資料庫查詢語言(如 SQL)。

  • 四種標準連接:

    • inner
    • full
    • left
    • right
  • 四種皆可用 merge() 完成。

R 的 data.table 資料合併

Inner join(內連接)

只保留同時在兩個 data.table 中都有資料的觀測值。

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

R 的 data.table 資料合併

`by` 引數

by 可避免重複輸入相同的欄名。

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

R 的 data.table 資料合併

Full join(全連接)

保留出現在任一個 data.table 的所有觀測值。

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

R 的 data.table 資料合併

一起來練習吧!

R 的 data.table 資料合併

Preparing Video For Download...