如何压缩CSV文件并将压缩后的文件使用R写入文件夹 [英] How do I zip a csv file and write that zipped file to a folder using R
问题描述
我有一个R脚本,可以在计算后生成将近80000 KB的csv文件。我想将此csv文件写到文件夹 D:/ My_Work / Output
,文件名为 result.zip
压缩文件。请建议是否有任何功能或任何方式可以实现此目的。
I have an R script which generates a csv file of nearly 80000 KB after calculations. I want to write this csv file to folder say D:/My_Work/Output
with file name result.zip
as a zipped file. Please suggest is there any function or any way that i could achieve this.
推荐答案
使用 zip
函数:
zip(*path to zip*,*path to csv*)
编辑:不幸的是,您不能从data.frame直接转到压缩的csv。您需要显式地制作csv,但是编写一个删除csv的包装程序并不难,这样您就永远不会像这样:
edit: Unfortunately you cannot go from data.frame straight to zipped csv. You need to explicitly make the csv, but it wouldn't be hard to write a wrapper that deletes the csv so that you never know its there like so:
zipped.csv <- function(df, zippedfile) {
# init temp csv
temp <- tempfile(fileext=".csv")
# write temp csv
write.csv(df, file=temp)
# zip temp csv
zip(zippedfile,temp)
# delete temp csv
unlink(temp)
}
这篇关于如何压缩CSV文件并将压缩后的文件使用R写入文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!