fread not reading quoted columns as character
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 42/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Rdatatable/data.table
-
as.data.table() recurses without end on a survival::Surv object (or any data.frame carrying one) Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Rdatatable/data.table#7887 ·
-
consistency tests
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#7853 · 3 comments ·
-
internals
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#6938 · 1 comment ·
-
encoding fread
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Rdatatable/data.table#5179 · 8 comments ·
-
documentation programming
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Rdatatable/data.table#3199 · 3 comments ·
All issues in Rdatatable/data.table
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
r-lib/pkgdepends#485 · 3 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
beginners blocker
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enviPathR OpenBuild Error Build OK Build Warning policies-accepted pre-review precheck-passed
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Bioconductor/BiocContributions#207 · 6 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
datacarpentry/semester-biology#1255 ·