Appending the row to data.table works differently than in data.frame: How and why?

Convert to list and it should work

rbind(team_table,as.list(1:11))
#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
#1: NA NA NA NA NA NA NA NA NA  NA  NA
#2:  1  2  3  4  5  6  7  8  9  10  11

It is also the same behavior with data.frame

rbind(team_table,as.list(1:11))
#  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
#1 NA NA NA NA NA NA NA NA NA  NA  NA
#2  1  2  3  4  5  6  7  8  9  10  11

Regarding why it fails, rbind in data.table is calling rbindlist and it is considering the vector (1:11) as a single column.

rbind(team_table,1:11)

Error in rbindlist(l, use.names, fill, idcol) : Item 2 has 1 columns, inconsistent with item 1 which has 11 columns. To fill missing columns use fill=TRUE.

If we convert it to a list with 11 element (data.frame or data.table are list with list elements i.e. columns having same length), it works because it would consider the number of columns to be the same


Since 1:11 is being considered as a column, we can transpose the values to rbind it as a row

library(data.table)
team_table <- as.data.table(matrix(NA,ncol = 11))
rbind(team_table,t(1:11))

#   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
#1: NA NA NA NA NA NA NA NA NA  NA  NA
#2:  1  2  3  4  5  6  7  8  9  10  11