如何提取多个 zip 文件并在 R 中读取这些 csvs? [英] How can I extract multiple zip files and read those csvs in R?
问题描述
我需要一次解压缩多个文件并将其作为数据框添加到我的 R 仪表板中.
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.
请帮忙.
推荐答案
我经常使用 plyr 包中的 ldply 函数来读取或处理多个文件.
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)
我非常喜欢乔尔的评论.太习惯用plyr包了,忘了你也可以用sapply函数.也许更好用!
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!
这篇关于如何提取多个 zip 文件并在 R 中读取这些 csvs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!