fread fails for properly quoted \n in file with \r as EOL

Open
#2,943 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reproducing the minimal example with fread() and the linked dt-test.csv input, then compare its parsing with data.table 1.10.4-3. Trace the fread() handling of carriage returns, quoted fields, and row boundaries. Done means the ^M separators create rows while carriage returns inside quoted cells do not, matching the expected four-row output.

Written by the indexing model from the issue text.

Description

bug fread regression

I described this on stackoverflow, Hugh suggested it might be a valid issue to report here (https://stackoverflow.com/questions/50918972/data-table-fread-does-not-see-m-as-new-line-any-more)

This is a new problem with data.table 1.11.4, did not occur in 1.10.4-3.

Data sets distributed from Qualtrics sometimes have both "^M" to mark new lines and also there are "naked" carriage returns in the middle of data cells. This confuses R read.csv horribly, we changed over to fread and it has worked in past. Now it is not working. In all of the cases I have tested, the problem is in row 2 of the data frame where the questions are repeated and when the Qualtrics user includes a carriage return in their cut/paste, then that return is preserved in the data. I made an MRE data set, http://pj.freefaculty.org/scraps/dt-test.csv

Another element in this mess is the new-ish Excel style of quotes for character variables, which do not include quotation marks unless absolutely necessary. We see some cell entries with quotes and others without in the row 2 information.

I do not want to preserve the carriage returns that are in the middle of the cells. I want the "^M" to be seen as new lines.

# Minimal reproducible example

library(data.table)
fread("http://pj.freefaculty.org/scraps/dt-test.csv")

The output is as follows.
Note that the things that show as "^M" in Emacs are seen as "\r" in fread error message:

   id,y,x1,x2\rRespondentidentification,      "An
1:                            variable", "Working
2:                               input",    "more
                                  outcome
1:                                     on
2: input"\rResp1,33,22,33\rResp2,31,19,32
Warning message:
In fread("http://pj.freefaculty.org/scraps/dt-test.csv") :
  Found and resolved improper quoting in first 100 rows. If the fields are not quoted (e.g. field separator does not appear within any field), try quote="" to avoid this warning.

Desired result, which happened in previous version of data.table, is a new data object, with the CSV rows separated by the "^M" thing and ignoring the carriage returns in the middle of the input data for row 2.

id,y,x1,x2
Respondentidentification, "An outcome variable", "Working on input", "more input"
Resp1,33,22,33
Resp2,31,19,32

I expect these will be all character because of the questions in row 2.

I am not allowed to publish the real Qualtrics data set in this forum, but if one of the data.table developers is willing to write me at pauljohn@ku.edu, I'll email a copy if you will agree not to redistribute. But I do believe the MRE is representative.

# Output of sessionInfo()

R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

other attached packages:
[1] data.table_1.11.4

loaded via a namespace (and not attached):
[1] compiler_3.4.4
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.