22

I have a big data.frame that I want to write into a compressed CSV file. Is there any way to directly write the data into a CSV.TAR.GZ compressed file instead of performing write.csv/gzip steps in order to reduce DISK access?

Thanks.

imriss
  • 1,815
  • 4
  • 31
  • 46

1 Answers1

48

Use gzfile (or bzfile for bzip2 archiving, or xzfile for xz archiving).

write.csv(mtcars, file=gzfile("mtcars.csv.gz"))

PS. If you only have one data frame, surely you don't need tar.

dfrankow
  • 20,191
  • 41
  • 152
  • 214
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • 2
    Thanks. Does it need a close()? – imriss Jul 05 '13 at 16:39
  • At least on Windows, no; and in fact, trying to do anything with the connection after the file has been written gives an error. – Hong Ooi Jul 05 '13 at 16:46
  • 1
    Good to know. Maybe, it is a bug that should be fixed. – imriss Jul 05 '13 at 17:10
  • 8
    More concise writing. write.csv(mtcars, file=gzfile("mtcars.csv.gz")) – Ven Yao Oct 18 '15 at 07:54
  • Can you please help me understand something regarding the line above? I did the same, but my OS won't allow me to open the GZ file saying it is broken. Error message: "Inappropriate file type or format." OS is MacOS Big Sur 11.2.3. It worked on the first try, but ever since I couldn't open a gz file written this way. – Programmer Apr 11 '21 at 15:14