data.tableの結合

R で学ぶ data.table によるデータ結合

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

同じ列・異なるdata.table

data.tableの結合

R で学ぶ data.table によるデータ結合

結合関数

rbind():異なる変数に格納されたdata.tableの行を結合

rbindlist()data.tablelistから行を結合

R で学ぶ data.table によるデータ結合

rbind()関数

変数として格納された2つ以上の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()関数

data.tablelistから行を結合

# 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 = FALSEfill = TRUEの場合を除く)
R で学ぶ data.table によるデータ結合

練習しましょう!

R で学ぶ data.table によるデータ結合

Preparing Video For Download...