otwtm
otwtm

Reputation: 1999

fread - Specify data type of one specific column

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.

Upvotes: 4

Views: 2768

Answers (1)

otwtm
otwtm

Reputation: 1999

As Roland pointed out in a comment, we can use the argument colClasses "with a named vector specifying types for a subset of the columns by name".

Hence, in the above mini example we can do somthing like:

df = fread(file="path/to/my_file.csv", colClasses = c('id'='character'))

Upvotes: 2

Related Questions