How do I zip a csv file and write that zipped file to a folder using R

假装没事ソ 提交于 2019-12-12 03:15:14

问题


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.


回答1:


Use the zip function:

zip(*path to zip*,*path to 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)
}



回答2:


If you want just save some space on the disk then it is more convenient to use *.gz compression.

write.csv(iris, gzfile("iris.csv.gz"), row.names = FALSE)
iris2 = read.csv("iris.csv.gz")


来源:https://stackoverflow.com/questions/38487628/how-do-i-zip-a-csv-file-and-write-that-zipped-file-to-a-folder-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!