Lucas Morin
Lucas Morin

Reputation: 398

R - type detection / conversion

I have a big R data.table, and I am trying to reduce it's size. fwrite and fread allow for automatic type detection (and reducing size in the process). However it has some funky behavior (reading "0001" as a number and converting it to 1).

Are there some automated option for automatic type detection / conversion without losses ?

Upvotes: 0

Views: 42

Answers (2)

user12728748
user12728748

Reputation: 8506

If you want "0001" to be read as a character; check out colClasses in ?fread; it allows you to define that for individual columns, such as:

library(data.table)
fread('
1, 0001
2, 0002
', colClasses=c(V2="character"))
#>    V1   V2
#> 1:  1 0001
#> 2:  2 0002

Upvotes: 1

IceCreamToucan
IceCreamToucan

Reputation: 28675

There is an argument for this

library(data.table)

fread('
1, 0001
2, 0002
') 
#    V1 V2
# 1:  1  1
# 2:  2  2

fread('
1, 0001
2, 0002
', keepLeadingZeros = TRUE) 
#    V1   V2
# 1:  1 0001
# 2:  2 0002

Upvotes: 2

Related Questions