I have a .csv where a column of IDs contains a long integer with leading zeros. fread
converts it into an integer64
type. How would I specify the class for one column and then just let fread
automatically guess the classes for the remaining columns? Not sure if this is an "all-or-nothing" type of situation.
I have 50+ columns and would rather not have to specify the data types for all of them just because I have to do so for one of them.
My question is related to: R fread - read all columns as character.
From ?fread
:
# colClasses
data = "A,B,C,D\n1,3,5,7\n2,4,6,8\n"
fread(data, colClasses=c(B="character",C="character",D="character")) # as read.csv
fread(data, colClasses=list(character=c("B","C","D"))) # saves typing
fread(data, colClasses=list(character=2:4)) # same using column numbers
That is, if your zero-padded column is called big_num
, just use colClasses = list(character = 'big_num')
Addressing the auto detection and overriding a specific column:
# Auto detect the column types (special case of using nrows=0)
colCls <- sapply(fread(fName, nrows=0), class)
# Override the "wrong" detected column types
colCls[c("field1", "field2")] <- "character"
dt<-fread(fName, colClasses = colCls)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With