Reputation: 823
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:
fread
to maintain these line breaks at all?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
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