How do I use compressed data with R?
R supports two primary ways of accessing compressed data. This allows you to keep your data files on disk compressed saving space, and often time (since the file I/O saved by compression is often more expensive than the cpu cycles it uses)
If you are storing your data in native format, simply use the
compress option of
tst.df=as.data.frame(cbind(1:10,2:11)) # just some testing data save(tst.df,file="test.Rbin", compress=T) # save a compressed R file
You can use
load as normal, to read the compressed files:
To access any other kind of file with compression, simply use
gzfile("") around the file name:
write.table(tst.df,gzfile("test.dat.gz")) # write a compressed file read.table(gzfile("test.dat.gz"),row.names=1)# read it back in
Files compressed using the gzfile method can also be compressed and uncompressed using the UNIX
gunzip commands (respectively).