首页 > 解决方案 > R从Zip文件中的文件模式创建DataFrame

问题描述

我已经能够使用此代码下载以下 .zip 文件

library(curl)
file_destination <- "C:/Documents/c3.zip"
curl_fetch_disk(url = "https://api.esios.ree.es/archives/9/download?date_type=datos&start_date=01-01-2019&end_date=31-01-2019",
                    path = file_destination )

我错过了什么?

1)我只想下载到 Temp.file而不是本地目录,我想用 file_destination 替换

   file_destination <- tempfile()

  

2)我需要从与此名称模式 "C3_cuotaven*.csv" 匹配的所有 CSV 文件中创建一个数据框:

   2.1 name starts with "C3_cuotaven" 
 
   2.2 and ends with ".csv"

请帮忙

标签: r

解决方案


我已经能够产生这个解决方案

library(curl)
# Create a temporarly file and Temporaly directory
temp_direct <- tempdir()
temp <- tempfile()

#Download ZIP file from web
curl_fetch_disk(url = "https://api.esios.ree.es/archives/9/download?date_type=datos&start_date=01-01-2020&end_date=31-08-2020",
                path = temp)
# Unzip downloaded file in temporaly directory, this creates several ZIP files in it.
unzip(temp, exdir =temp_direct )

# Create a list of files that contain C3 in filename.
zip_c3_pattern <- list.files(path = temp_direct, pattern = '*C3*', full.names = TRUE)
library(plyr)

# unzip all your files with previous pattern
llply(.data = zip_c3_pattern, .fun = unzip, exdir = temp_direct)

# Create a vector list with files that contain this key in their name "C3_cuotaven"
csv_cuotaven <- list.files(path = temp_direct, pattern = '*C3_cuotaven*', full.names = TRUE)

library(plyr)

# Create a dataframe with files that are contain in csv_cuotaven following name pattern = "c3_cuotaven"
library(data.table)
c3_merge <- rbindlist(lapply(csv_cuotaven,  
                             function(x) cbind(fread(x, sep=';', header=FALSE,
                                                     stringsAsFactors=FALSE),
                                               file_nm=x)), fill=TRUE )
#Remove temporaly file from memory
unlink(temp)

# Remove files from directory that start with C3 or file
files <- list.files(temp_direct, full.names = T, pattern ="^C3|^file")
file.remove(files)

推荐阅读