1

I have a text file that looks something like this:

a,b,c,d
"string1","string2","string3","
"string4","string5","string6","

The file itself is comma separated, but each line ends with a double quote (i.e., not the comma delimiter). readr::read_delim() does not understand the line endings and thus tries to read all data into a single line.

data.table::fread() imports as expected, but I'd like to find a readr solution, if it exists.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Chris
  • 51
  • 5

2 Answers2

1

Using fread seems by far the easiest option in this case. If you don't want fread to return a data.table, you can use the data.table = FALSE parameter in fread.

An example:

fread("C:/data.txt", data.table = FALSE)
Jaap
  • 81,064
  • 34
  • 182
  • 193
  • I'm not opposed to data.table, but was curious whether I had missed something obvious in the `readr` package documentation. Line endings are detected automatically in `data.table`, which does not seem to be the case with `readr`. – Chris May 08 '18 at 21:42
  • @Chris As far as I can see (and I've looked extensively) you didn't miss anything in the documentation of `readr`. I guess the `readr`-functions are less flexible than `fread`, which has its advantages and disadvantages (like in this case). – Jaap May 09 '18 at 09:16
0

Here's one long-winded approach, but surely there is a more parsimonious solution:

readr::read_lines("C:/data.txt", skip = 1) %>% 
  tibble::as_tibble() %>% 
  tidyr::separate(value, into = c("a", "b", "c", "d"), sep = ",") %>% 
  mutate_at(.vars = vars(a:c), 
            .funs = stringr::str_replace_all, 
            pattern = "\"", 
            replacement = "")
Chris
  • 51
  • 5
  • That's a lot of code to avoid using `fread`. If you don't want to use it because `fread` returns a `data.table`, see my answer for a solution. – Jaap May 07 '18 at 19:50
  • In cases where my data include delimiters or other "reserved" characters, I've moved away from read.csv and gravitated to readXL to avoid mis-specification during the import process. Perhaps consider a different data storage option? – Ben May 08 '18 at 17:32
  • I agree it's a lot of code. Using `data.table` works and is much more parsimonious, but was hopeful I missed something in `readr`. – Chris May 08 '18 at 21:43