How to Directly Perform Write.CSV in R into Tar.Gz Format

How to directly perform write.csv in R into tar.gz format?

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.

R directly save data as zip file

It's possible using gzip.

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

How can write.csv and read.csv preserve the data format?

By default write.csv will include headers, but when you are importing them you are telling R that there are no headers. It's likely that those headers are non-numeric which triggers the switch to character. So either turn off the headers in the write.csv() or set header=TRUE in the read.csv()

Is possible to use fwrite from `data.table` with gzfile?

You can use the gzip function in R.utils:

library(R.utils)
library(data.table)

#Write gzip file
df <- data.table(var1='Compress me',var2=', please!')
fwrite(df,'filename.csv',sep=',')
gzip('filename.csv',destname='filename.csv.gz')`

# Read gzip file
fread('gzip -dc filename.csv.gz')
var1 var2
1: Compress me , please!


Related Topics



Leave a reply



Submit