[R] How to unzip a .gz file

Henrik Bengtsson hb at biostat.ucsf.edu
Wed Feb 11 02:11:54 CET 2015


Not clear if you need to:

1. decompress /home/file.gz, or
2. read the content of /home/file.gz into R.

For (1) you can use `gunzip` at the command line, or
gunzip("/home/file.gz") of the R.utils package.  For (2), as already
mentioned, R does a good job of reading gzip'ed files "as is".  It may
even be that you don't have to use gzfile().

DEMO:

# Create gzip'ed file
> cat(file="foo.txt", "Hello world!\n")
> R.utils::gzip("foo.txt")

# Read directly from it
> readLines("foo.txt.gz")
[1] "Hello world!"

# Decompress it
> R.utils::gunzip("foo.txt.gz")
> readLines("foo.txt")
[1] "Hello world!"

/Henrik

On Tue, Feb 10, 2015 at 1:45 PM, Alexandra Catena <amc5981 at gmail.com> wrote:
> Hello,
>
> Can someone help me with unzipping a .gz file.  I used:
>
> readLines(gzfile('/home/file.gz'))
>
>
> I also found that I could use gunzip, but after trying to install it, it
> says:
>
>  "package ‘gunzip’ is not available (for R version 2.15.1)"
>
>
> Thanks,
> Alexandra
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list