串接 data.table

R 的 data.table 資料合併

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

相同欄位、不同 data.table

串接 data.tables

R 的 data.table 資料合併

串接函式

rbind():串接不同變數中的 data.table

rbindlist():串接 list 中多個 data.table 的列

R 的 data.table 資料合併

rbind() 函式

串接作為變數儲存的兩個以上 data.table

# ... 可接任意數量引數
rbind(...) 
rbind(sales_2015, sales_2016)
   quarter  amount
1:       1 3200100
2:       2 2950000
3:       3 2980700
4:       4 3420000
5:       1 3350000
6:       2 3000300
7:       3 3120200
8:       4 3670000
R 的 data.table 資料合併

加入識別欄

idcol 參數會新增一欄,標示來源 data.table

rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year")
   year quarter  amount
1: 2015       1 3200100
2: 2015       2 2950000
3: 2015       3 2980700
4: 2015       4 3420000
5: 2016       1 3350000
6: 2016       2 3000300
7: 2016       3 3120200
8: 2016       4 3670000
R 的 data.table 資料合併

加入識別欄

rbind(sales_2015, sales_2016, idcol = "year")
   year quarter  amount
1:    1       1 3200100
2:    1       2 2950000
3:    1       3 2980700
4:    1       4 3420000
5:    2       1 3350000
6:    2       2 3000300
7:    2       3 3120200
8:    2       4 3670000
R 的 data.table 資料合併

加入識別欄

rbind(sales_2015, sales_2016, idcol = TRUE)
   .id quarter  amount
1:   1       1 3200100
2:   1       2 2950000
3:   1       3 2980700
4:   1       4 3420000
5:   2       1 3350000
6:   2       2 3000300
7:   2       3 3120200
8:   2       4 3670000
R 的 data.table 資料合併

處理缺少的欄位

rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year", 
      fill = TRUE)

R 的 data.table 資料合併

處理缺少的欄位

rbind(sales_2015, sales_2016, idcol = "year")
Error in rbindlist(l, use.names, fill, idcol) : 
  Item 2 has 3 columns, inconsistent with item 1 which has 2 columns. 
  If instead you need to fill missing columns, use set argument 'fill' 
  to TRUE.
R 的 data.table 資料合併

rbindlist() 函式

listdata.table 串接列

# 讀入 data.table 清單
table_files <- c("sales_2015.csv", "sales_2016.csv")
list_of_tables <- lapply(table_files, fread)
rbindlist(list_of_tables)
   quarter  amount
1:       1 3200100
2:       2 2950000
3:       3 2980700
4:       4 3420000
5:       1 3350000
6:       2 3000300
7:       3 3120200
8:       4 3670000
R 的 data.table 資料合併

加入識別欄

idcol 參數會從輸入清單的名稱取值

names(list_of_tables) <- c("2015", "2016")
rbindlist(list_of_tables, idcol = "year")
   year quarter  amount
1: 2015       1 3200100
2: 2015       2 2950000
3: 2015       3 2980700
4: 2015       4 3420000
5: 2016       1 3350000
6: 2016       2 3000300
7: 2016       3 3120200
8: 2016       4 3670000
R 的 data.table 資料合併

處理不同欄位順序

rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year", 
      use.names = TRUE)

R 的 data.table 資料合併

不同欄名的 `data.table`

rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year", 
      use.names = FALSE)

R 的 data.table 資料合併

`use.names = FALSE` 的陷阱

rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year", 
      use.names = FALSE)

R 的 data.table 資料合併

不同的預設值

  • rbind() 的預設是 use.names = TRUE
  • rbindlist() 的預設是 use.names = FALSE,除非設 fill = TRUE
R 的 data.table 資料合併

一起來練習吧!

R 的 data.table 資料合併

Preparing Video For Download...