I am using data.table::fread to read a csv-file. Is there any way to specify the type of just one column and let fread infer all the other columns?
Background: I have a csv-file with about 60 columns. For all but one column fread infers the right data type. But then there is one column which is an id-column with leading zeros, which should be read as character but is parsed as numeric removing the leading zeros.
Mini Example:
csv file:
id, size, weight
001, 180, 75
0001, 190, 90
002, 160, 58
desired data.table:
df = data.table(id=c("001", "0001", "002"), size=c(180, 190, 160), weight=c(75, 90, 58))
I know I could use the colClasses argument to specify a list of column classes, but I don't want this, because fread infers all but one column correctly.
I cannot df[,id] <- as.character(df[,id]), since the information is lost by removing the leading zeros.
colClasses = c('id'='character'). - otwtm