rcsvdplyrwrite.table

Writing to a Csv with Quotation Marks Present


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:

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")