Read Compressed Zip Files in R

[This article was first published on Brock's Data Adventure » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: Brock's Data Adventure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)