Search code examples
rdata.tablefwritefread

How to write ill-formatted header lines to a csv with R data.table fwrite?


I have the following *csv file, with has three "header" fields which are formatted differently than a csv file, example.csv:

# Filename is addresses.csv
# Location of file from https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html
# The data isn't authentic
John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

Normally, I would read in the file with R data.table, skipping the initial lines.

library(data.table)
dt = fread('addresses.csv', skip=3)
## do something

After analyzing/manipulating the data, I normally write the file as the following:

fwrite(dt, "result.csv", col.names=FALSE)

The problem with this is that, I have erased the original header lines.

Is there a standard approach for reading in the original header lines, and appending them to the final result to write via fwrite()? I think there will be parsing errors if I append these header lines to dt before using fwrite()

EDIT: Note I would like the header lines to be preserved, i.e.

# Filename is addresses.csv
# Location of file from https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html
# The data isn't authentic

Solution

  • You can start by writing the hearder lines from the original file with write(header_lines, filename), followed by your fwrite command with the option append=T.