How to append a whole dataframe to a CSV in R

Another workaround (using cat) is that you transpose your data frame, then append a row containing only "\n" using rbind, then converting this data frame to a vector (it will have "\n" after every 'row') which will now show up in your csv file as a data frame.

Below an example:

df <- rbind(t(df),"\n")
cat(c("",df),sep=",",append=TRUE)

Note: my assumption is that you have opened a connection to a file using sink(filepath/filename.csv).


Ok so I realised that append=T does work with write.table - but write.table needs the sep switch to be used so this works:

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

Tags:

Csv

R

Dataframe