Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm running an R session in an ssh server, and I have limited storage capacity. I was wondering if there is an implementation of fwrite that allows compression? Something like:

z <- gzfile("file.csv.gz")
fwrite(object, z)
like image 589
Mario GS Avatar asked Mar 14 '17 14:03

Mario GS


2 Answers

UPDATE: As of 2019-10-03 the below change is also released on CRAN (starting with version 1.12.4), so a simple install.packages("data.table") should now suffice.

data.table now supports fwrite() directly to gzipped csvs. As of Aug 28 2019 this is not released on CRAN, but you can get it by installing from GitHub:

# Install development version of data.table
install.packages("data.table", repos="https://Rdatatable.gitlab.io/data.table")

# Generate some data
library(data.table)
dt <- data.table(
  state = sample(state.name, size = 1e6, replace = TRUE),
  measure = runif(1e6)
)

# Write to a gzipped file
data.table::fwrite(dt, file = "dt.gz")

# Read back
library(R.utils)
dt2 <- data.table::fread("dt.gz")
like image 184
Jozef Avatar answered Sep 20 '22 22:09

Jozef


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!
like image 22
user3055034 Avatar answered Sep 24 '22 22:09

user3055034