Exporting a file as a pipe delimited file

Hello All,

I have a scenario I am facing.

I need to Export a Pipe Delimited Text File From R with a header and trailer.

Due to the sensitivity of the data, I am working with, I provided a mock of what could be done with the output below.

employee <- c('John Doe','Peter Gynn','Jolie Hope')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))

employ.data <- data.frame(employee, salary, startdate)

employ.data

The DF created the following output:

employee salary startdate
John Doe 21000 2010-11-01
Peter Gynn 23400 2008-03-25
Jolie Hope 26800 2007-03-14

code used to export the DF as a pipe delimited file:

write_delim(employ.data, file = "EIN01_Pipe_delimited.txt", delim = "|")

I believe I am close as when I used the code above export the DF to a pipe delimited format, I get the following:

employee|salary|startdate
John Doe|21000|2010-11-01
Peter Gynn|23400|2008-03-25
Jolie Hope|26800|2007-03-14

What I need is below in which you will see it shows the header, name, number of rows. Then the body displays all of the columns followed by all the rows of data.

HD|EIN01|3
Patient ID - Pharmacy|Patient Gender|Patient Ethnicity|PHI Consent?|Patient State|Patient Birth Year|Patient Height|Patient Weight|PHI Consent Date|Patient MRN|Patient Phone - Pri|Patient Phone - Sec|Patient SSN|Patient Email|Patient Hub ID|Patient Last Name|Patient First Name|Patient Address1|Patient Address2|Patient City|Patient Postal Code|Patient Birth Date
Z1079321|M|6||MN|1979||||00961922||||||Ruth|Babe|2625 1ST AVE||Warroad|54321|19791004
Z1089101|F|6||MN|1984||||00838657||||||Gehrig|Lou|241 FAIR RD||Roseau|54322|19840716
Z1090762|F|6||MN|1957||||01432021||||||Cobb|Ty|2717 MAIN Rd.|#17|Baudette|54333|19571012
TR|EIN01

I think I am very close, but I am missing some last key parts.

Is there anyone that could help me round out this task?

All the help is greatly appreciated.

Thank you!

I'm not completely clear what you're asking, but it sounds as though you want to write a customer header before you write your pipe delimited file and then add a custom footer.

You can write to a file with the write function in base R. Use this to write your custom header then use write_delim() as before but with the append = TRUE parameter so that the data is added to the file with your header. Finally, use write() again, but this time also with the append = TRUE parameter so that footer is added to the existing file.

For example, given you small example data set above, you would do this:

write(
  paste0("HD|EIN01|", nrow(employ.data)), 
  file = "EIN01_Pipe_delimited.txt", 
  append = FALSE. # Create a new file
)

write_delim(
  employ.data, file = "EIN01_Pipe_delimited.txt", 
  delim = "|", 
  append = TRUE  # Add to the existing file
)

write(
  "TR|EIN01", 
  file = "EIN01_Pipe_delimited.txt", 
  append = TRUE  # Add to the existing file
)
1 Like

Hello and thank you so much for your help.

I apologize in advance for the poor language in trying to convey my question.

There is just one element missing.

How do you include the column headers as well?

What was provided indeed helped with getting the header, numbers and trailer, but I need to include the column header titles as well.

Something like this:

HD|EIN01|3
Name|Salary|startdate
John Doe|21000|2010-11-01
Peter Gynn|23400|2008-03-25
Jolie Hope|26800|2007-03-14
TR|EIN01

As the current output shows this:

HD|EIN01|3
John Doe|21000|2010-11-01
Peter Gynn|23400|2008-03-25
Jolie Hope|26800|2007-03-14
TR|EIN01

Once again, I appreciate your help!

Thank you!

I apologize, my mistake. That's a quick fix in write_delim(), just add col_names = TRUE. Here's a revised edit to the code above:

write_delim(
 employ.data, file = "EIN01_Pipe_delimited.txt", 
 delim = "|", 
 append = TRUE,  # Add to the existing file,
 col_names = TRUE # Include the variable names in the output
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.