歡迎加入本課程

R 的 data.table 資料合併

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

連接 data.table

  • 將兩個 data.table 的資訊合併成單一 data.table

R 的 data.table 資料合併

課程導覽

  • 第 1 章:用 merge() 連接資料

  • 第 2 章:data.table 工作流程中的連接

  • 第 3 章:連接除錯

  • 第 4 章:串接與重塑 data.table

R 的 data.table 資料合併

表格鍵(key)

跨兩張表連結資訊的欄位

library(data.table)

demographics <- data.table(name = c("Trey", "Matthew", "Angela"), gender = c(NA, "M", "F"), age = c(54, 43, 39)) shipping <- data.table(name = c("Matthew", "Trey", "Angela"), address = c("7 Mill road", "12 High street", "33 Pacific boulevard"))

R 的 data.table 資料合併

在 R 工作階段檢視 `data.table`

tables() 會顯示你 R 工作階段中已載入的所有 data.table

tables()
           NAME NROW NCOL MB            COLS KEY
1: demographics    3    3  0 name,gender,age    
2:     shipping    3    2  0    name,address    
Total: 0MB
R 的 data.table 資料合併

在 R 工作階段檢視 `data.table`

str() 會顯示單一 data.table 各欄位的型別

str(demographics)
Classes 'data.table' and 'data.frame':    3 obs. of  3 variables:
 $ name  : chr  "Trey" "Matthew" "Angela"
 $ gender: chr  NA "M" "F"
 $ age   : num  54 43 39
 - attr(*, ".internal.selfref")=<externalptr> 
R 的 data.table 資料合併

在 R 工作階段檢視 `data.table`

demographics_all
         name sex age
  1:     Trey  NA  54
  2:  Matthew   M  43
  3:   Angela   F  39
  4: Michelle   F  63
  5:  Mohamed   M  26
 ---                 
102:  Patrick   M  27
103:      Wei   F  41
104:     Adam   M  33
105:  Somchai   M  53
106:     Alma   F  19
R 的 data.table 資料合併

一起來練習吧!

R 的 data.table 資料合併

Preparing Video For Download...