连接 data.table

在 R 中使用 data.table 进行数据表连接

Scott Ritchie

Postdoctoral Researcher in Systems Genomics

同列,不同 data.table

连接 data.table

在 R 中使用 data.table 进行数据表连接

连接函数

rbind(): 连接存于不同变量的 data.table 的行

rbindlist(): 连接 listdata.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() 函数

list 中的 data.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 进行数据表连接

Passons à la pratique !

在 R 中使用 data.table 进行数据表连接

Preparing Video For Download...