我需要一次解压缩多个文件并将其添加为 R 仪表板中的数据框。
我目前正在使用此代码:
zipF<- "/Users/sahilverma13/Desktop/chat_data_2017-01-30_IST.zip"
outDir<-"/Users/sahilverma13/Desktop"
unzip(zipF,exdir=outDir)
Run Code Online (Sandbox Code Playgroud)
但是我必须分别为每个文件执行此操作。
zipF <- list.files(pattern="*.zip")
Run Code Online (Sandbox Code Playgroud)
我尝试使用通配符,但它不起作用。
请帮忙。
我经常使用 plyr 包中的 ldply 函数来读取或处理多个文件。
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)
Run Code Online (Sandbox Code Playgroud)
正如理查德指出的那样,这并不完整(早上喝太多咖啡也不好)。
# 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)
Run Code Online (Sandbox Code Playgroud)
我非常喜欢乔尔的评论。太习惯用plyr包了,忘了你也可以用sapply函数。也许更好用!