Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Writing to a Csv with Quotation Marks Present – R

I am running into an issue when using write.table(). I am writing a dataframe to a csv, and any time a column has a " quotation mark in it, the data is not writing how I’d like.

I am trying to write this dataframe to a csv:

activity          date         status
widen table by 5" 01-05-2022     Y
router holes      01-25-2022     G
cut wood          02-03-2022     R

The activity in the first row is writing strangely for me. When I write it to a csv, it appears as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

activity               date         status
widen table by 5\",P" 01-05-2022     Y
router holes          01-25-2022     G
cut wood              02-03-2022     R

This is the code I am using to write to a csv:

write.table(df, "df.csv", sep = ",", row.names = FALSE, col.names = !file.exists("df.csv"), 
append = T)

>Solution :

The simplest thing to do is to use write.csv instead of write.table.

write.csv(df, "df.csv",  row.names = F)

But if you really want to use write.table, you need to specify qmethod="double".

write.table(df, "df.csv", sep = ",", row.names = FALSE, 
    col.names = T, append = T, qmethod="double")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading