Rdatatable / data.table

R's data.table package extends data.frame:
http://r-datatable.com
Mozilla Public License 2.0
3.59k stars 978 forks source link

`fread` can't seem to use `col.names` and `colClasses` together #3816

Open xiaodaigh opened 5 years ago

xiaodaigh commented 5 years ago

I can't seem to use col.names and colClasses together in data.table::fread. See MWE

data.table::fwrite(cars, "cars.csv", col.names = FALSE)
data.table::fread("cars.csv", col.names = c("a","b"),
                  colClasses = list(a="character", b = "character"))

which produces the error

Error in data.table::fread("cars.csv", col.names = c("a", "b"), colClasses = list(a = "character", : Column name 'character' in colClasses[[1]] not found

MichaelChirico commented 5 years ago

This is as documented (emphasis mine):

colClasses | As in read.csv; i.e., an unnamed vector of types corresponding to the columns in the file

col.names | A vector of optional names for the variables (columns)... This is applied after check.names and before key and index.

I'm not sure it's possible to do otherwise? colClasses is passed to the file parser, and col.names is applied to the result of the file parser.

Also please let us know if you think the documentation can be improved 😄