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)
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")
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With