6

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.

joel.wilson
  • 8,243
  • 5
  • 28
  • 48
Sahil Verma
  • 63
  • 1
  • 1
  • 4

1 Answers1

8

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!

ricoderks
  • 1,619
  • 9
  • 13
  • This is half the problem, but does not read the csv files into R – Richard Telford Jan 31 '17 at 10:02
  • This works perfectly, Thanks! Except just one line # read the csv files my_data <- ldply(.data = csv_files, .fun = read.csv) This line doesn't seem to work. – Sahil Verma Feb 14 '17 at 10:53
  • Just to check, but : Have you installed the `plyr` library? and The `# read the csv files` is a commented line. Do you get any errors? – ricoderks Feb 14 '17 at 12:56
  • In the list.files, you may want to add `full.names = TRUE` if your files are not in the working directory. – ricoderks Feb 14 '17 at 13:09
  • In the list.files, alternatively, you can use `recursive = TRUE` to get all files in one directory and its sub-directory with full names – Julien Colomb Aug 19 '19 at 12:44
  • 1
    On the unzip command, you can also use base R: `lapply(zipF, unzip, exdir = outDir)`. ldply` can throw errors in certain instances. – MorrisseyJ Dec 09 '22 at 16:59