R의 data.table로 데이터 결합하기
Scott Ritchie
Postdoctoral Researcher in Systems Genomics
data.table 연결하기

rbind(): 별도 변수에 저장된 data.table의 행 연결
rbindlist(): data.table의 list에서 행 연결
변수로 저장된 두 개 이상의 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
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
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
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
rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year",
fill = TRUE)

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.
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
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
rbind("2015" = sales_2015, "2016" = sales_2016, idcol = "year",
use.names = TRUE)

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

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

rbind()의 기본값은 use.names = TRUErbindlist()의 기본값은 use.names = FALSE (단, fill = TRUE인 경우 제외)R의 data.table로 데이터 결합하기