fread not reading quoted columns as character

Open
#3,756 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the supplied R example with fwrite and fread, focusing on quoted character columns and the keepLeadingZeros argument. Compare the resulting types for var1, var2, var3, and var4. Done means fread preserves both numeric-looking character columns while continuing to infer var4 as integer.

Written by the indexing model from the issue text.

Description

fread

With older versions of data.table, I could convert variables that look like numeric fields into character. Then I would fwrite them, with the goal being to later fread them back with the data types preserved.

Here is a really simple data.table with the first three columns being character and the last integer.

library(data.table)

my_data <- data.table(
  var1 = c("001", "002", "003", "004", "005"),
  var2 = c("1", "2", "3", "4", "5"),
  var3 = c("a", "b", "c", "d", "e"),
  var4 = 1:5
)

str(my_data)

Classes ‘data.table’ and 'data.frame':	5 obs. of  4 variables:
 $ var1: chr  "001" "002" "003" "004" ...
 $ var2: chr  "1" "2" "3" "4" ...
 $ var3: chr  "a" "b" "c" "d" ...
 $ var4: int  1 2 3 4 5
 - attr(*, ".internal.selfref")=<externalptr> 

I want to be sure the data is saved with the proper formatting, so when I run fwrite I include the arguments quotes and qmethod like this:

fwrite(x = my_data, file = "my_data.csv", quote = TRUE, qmethod = "double")

I now fread the data back in and inspect the structure. Unfortunately, var1 and var2 are now integer when they should be character.

my_data_attempt1 <- fread(file = "my_data.csv")
str(my_data_attempt1)

Classes ‘data.table’ and 'data.frame':	5 obs. of  4 variables:
 $ var1: int  1 2 3 4 5
 $ var2: int  1 2 3 4 5
 $ var3: chr  "a" "b" "c" "d" ...
 $ var4: int  1 2 3 4 5
 - attr(*, ".internal.selfref")=<externalptr> 

I have found that one specific argument, keepLeadingZeros, does work to solve the issue for var1, but var2 is coerced.

my_data_attempt2 <- fread(file = "my_data.csv", keepLeadingZeros = TRUE)
str(my_data_attempt2)

Classes ‘data.table’ and 'data.frame':	5 obs. of  4 variables:
 $ var1: chr  "001" "002" "003" "004" ...
 $ var2: int  1 2 3 4 5
 $ var3: chr  "a" "b" "c" "d" ...
 $ var4: int  1 2 3 4 5
 - attr(*, ".internal.selfref")=<externalptr> 

I'm showing this example on 1.12.3, but I'm also having the issue on 1.12.2.

> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.12.3
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.