如何将整个数据框附加到 R 中的 CSV [英] How to append a whole dataframe to a CSV in R
问题描述
我已经可以使用 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="
", append=TRUE)
但是,据我所知,您不能将 cat 与整个数据框(多列和多行)一起使用.
However as far as I know you cannot use cat with a whole dataframe (multiple columns and rows).
我这样做是因为我正在将许多 DF 写入 CSV,并且我想使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!