2
votes

A strange error occurs when printing a data.table, but only when the data.table size is over 100 (example below). The error disappears when print() is replaced with print.data.frame(). Hence, my guess is that print-->print.data.frame inheritance is recognized only when data.table size is no more than 100.

Can someone give an intuition why this happens? My R version is 3.1.2.

library(data.table)

print(data.table(x=1:100), row.names=F) <-this command prints data.table

print(data.table(x=1:101), row.names=F) <- this command gives an error as below.

Error in `rownames<-`(`#tmp#`, value = rep.int("", nrow(x))) :
  length of 'dimnames' [1] not equal to array extent
1

1 Answers

4
votes

You probably have your datatable.print.nrows option set to the default of 100.

getOption("datatable.print.nrows")
# [1] 100

You can change the number of printed rows by either changing the option's value or by using nrows in print.data.table(). The latter is probably the best way to go as it will not change the default value.

args(data.table:::print.data.table)
# function (x, topn = getOption("datatable.print.topn"), 
#     nrows = getOption("datatable.print.nrows"), row.names = TRUE, ...) 

This will work -

dt <- data.table(x = 1:101)
print(dt, nrows = nrow(dt), row.names = FALSE)