Hester Lyons
Hester Lyons

Reputation: 823

R, fread CSV - how to maintain line breaks in cell values?

I've got a CSV file that I am reading into an R script using fread. The resulting variable is a vector, which is what I need for the next step in my process. There are values in my CSV file such as 'Energy \nElectricity', and the intention is that these will be labels for a chart, with a line break between (in this case) 'Energy' and 'Electricity' for formatting reasons.

When I manually code the vector to be myVec <- c('Energy \nElectricity'), this works fine and the line break is maintained. When I read the data in using fread, however, the resulting vector is effectively c('Energy \\nElectricity'), i.e. the process has inserted an extra escape character and the formatting is lost.

My question is as follows:

  1. Is there a way to use fread to maintain these line breaks at all?
  2. If not, can I format them differently in my csv file?
  3. If not, can I use gsub or similar to remove the extra line break once the file has been read into a vector?

I have tried all manner of ways to implement gsub (and sub), but they either get rid of both escape characters, such as gsub("\\\\", "\\", myVec) which gives [1] "Energy nElectricity", or they throw an error. I think I am missing something obvious. Any help appreciated.

Upvotes: 0

Views: 502

Answers (1)

JBGruber
JBGruber

Reputation: 12420

If nobody comes up with a better solution, this is how you would clean it using gsub:

gsub("\\n", "\n", "Energy \\nElectricity", fixed = TRUE)

The fixed option ignores all regex characters and is also considerably faster than fixed = FALSE.

Upvotes: 1

Related Questions