问题描述
我已经可以使用 cat 将行添加到CSV了,这非常容易:
I can already append a row to a CSV using cat which makes that very easy:
cat("my row, 1, 2, 3, 4", "mydf.csv",sep="\n", append=TRUE)
据我所知,您不能在整个数据框(多列和多行)中使用cat.
However as far as I know you cannot use cat with a whole dataframe (multiple columns and rows).
之所以这样做,是因为我正在向CSV中写入许多DF,并且我想使用 write.table 附加多个CSV.数据框的列数均相同.
I am doing this because I am writing many DFs to a CSV and I want to append the multiple CSVs using write.table. The dataframes all have the same number of columns.
我考虑过要在行上循环以用cat编写,但这听起来并不是最好的方法-任何人都可以在R中做到这一点吗?
I thought about doing a loop over rows to write with cat but that doesn't sound like the best way - any one have good way of doing this in R?
推荐答案
好,所以我意识到append = T确实适用于write.table-但是write.table需要使用sep开关才能使用:
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)
这篇关于如何在R中将整个数据框附加到CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!