require(data.table) # devel built 2017-10-31
dt <- data.table()
attributes(dt)
# $class
# [1] "data.table" "data.frame"
#
# $row.names
# integer(0)
#
# $names
# character(0)
#
# $.internal.selfref
# <pointer: 0x0000000000310788>
#
setnames(dt, character(0))
# Error in setnames(dt, character(0)) : x has no column names
names(df) <- character(0) would work on a null data.frame. I think this should not error. The reason is that it avoids an unnecessary if (ncol(dt)) before to do setnames(dt, nm) where nm is the vector of names, which would be character(0) if dt had no cols.
Output of sessionInfo()
# R version 3.3.2 (2016-10-31)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows Server >= 2012 x64 (build 9200)
#
# locale:
# [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
# [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
# [5] LC_TIME=English_United States.1252
# attached base packages:
# [1] stats graphics grDevices utils datasets methods base
#
# other attached packages:
# [1] data.table_1.10.5
#
# loaded via a namespace (and not attached):
# [1] tools_3.3.2 yaml_2.1.14
names(df) <- character(0)would work on a null data.frame. I think this should not error. The reason is that it avoids an unnecessaryif (ncol(dt))before to dosetnames(dt, nm)wherenmis the vector of names, which would becharacter(0)ifdthad no cols.Output of sessionInfo()