Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extract multiple zip files and read those csvs in R? [duplicate]

Tags:

r

zip

I need to unzip multiple files at once and add it as a data frame in my R dashboard.

I'm currently using this code:

zipF<- "/Users/sahilverma13/Desktop/chat_data_2017-01-30_IST.zip"
outDir<-"/Users/sahilverma13/Desktop"
unzip(zipF,exdir=outDir)

But I have to do it for every file separately.

zipF <- list.files(pattern="*.zip")

I tried using the wildcard but it doesn't work.

Please help.

like image 779
Sahil Verma Avatar asked Jan 31 '17 09:01

Sahil Verma


1 Answers

I often use the ldply function from the plyr package to read or do stuff with multiple files.

library(plyr)

# get all the zip files
zipF <- list.files(path = "/your/path/here/", pattern = "*.zip", full.names = TRUE)

# unzip all your files
ldply(.data = zipF, .fun = unzip, exdir = outDir)

As Richard pointed out this is not complete (to much coffee in the morning is also not good).

# get the csv files
csv_files <- list.files(path = outDir, pattern = "*.csv")

# read the csv files
my_data <- ldply(.data = csv_files, .fun = read.csv)

I liked the comment of Joel a lot. I'am used to using the plyr package so much that I forgot that you can also use the sapply function. Maybe even better to use!

like image 68
ricoderks Avatar answered Oct 02 '22 15:10

ricoderks