Read Compressed Zip Files in R

June 25, 2010
By

(This article was first published on Brock's Data Adventure » R, and kindly contributed to R-bloggers)

One of the great things that I am learning about R is that it is really powerful as a data management tool.  I just found how to unzip files.  I could use Python for this in SPSS, but it just feels like it is more natural to do in R.  Of course, you have to change the paths and the file names, but you should get the hint.

Note:  Right now, this works on .zip files.  I wonder if other formats will work as well?

Also: I use Eclipse and StatET to develop my R code. Here is some help at Jeromy’s blog to get you up and running here.

Lastly: Thanks to Hadley Wickham for the help on how to search an entire directory using regex @hadleywickham

## Look at your current directory
## and set it to what you want it to be
## need forward slash on windows!!
getwd()
setwd("C:/Users/Brock/Documents/IPEDS/ENROLLMENT - Migration/")
getwd()

## get help on zipfiles
?unzip

## create a vector file names we want to extract from
files.temp <- c("EF2008C.zip","EF2006C.zip","EF2004C.zip","EF2002C.zip","EF2000C.zip","EF98_C.zip","EF1988_C.zip")

## create a loop to extract the files to the directory set above
## will build the file name of the extracted file
unzip("EF2008c.zip")
for (i in files.temp)
unzip(i)

## a quicker way that doesnt require that you know which files - just does all
## \ allows you to use the . in .zip, the . is a special character
## $ is tells the pattern to search is the end? not sure about this one
for (i in dir(pattern="\.zip$"))
unzip(i)

## clean up the memory and close
rm(list=ls())
q()
n


Filed under: How-to, R Tagged: R, rstats, unzip

To leave a comment for the author, please follow the link and comment on his blog: Brock's Data Adventure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , ,

Comments are closed.