r - 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"
请帮忙
解决方案
我已经能够产生这个解决方案
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)
推荐阅读
- java - Java,Stream,是否可以获得.filter找到的对象数量的int值?
- matplotlib - Matplotlib 散点图,来自 CSV 的数据是垂直绘制而不是散射
- testing - OKTA OAuth 登录页面的 Testcafe 问题
- javascript - 打字稿 - 索引签名交叉问题
- python - 在 python 扩展中安全地处理大量数字
- javascript - 如何在选中或未选中复选框时从数组中推送和删除项目?
- python - 使用 selenium 抓取网站的价格
- r - 由于奇点,R-3 未定义
- odoo - 类型错误:create() 在尝试创建新记录时恰好采用 2 个参数(给定 1 个)
- wordpress - 使用电话号码而不是用户名登录 WordPress