英文:
reading in local files with different extensions - efficient way?
问题
我有一个包含不同文件扩展名(.csv、.xls、.xlsx)的20多个数据文件的列表。我想将每个数据文件存储为一个数据框
(data frame)并放入一个列表
中。我编写了一个for循环来执行这个任务。
我的“data”文件夹包含不同数据文件的子文件夹。然后在循环内,根据文件扩展名的不同调用不同的函数。我想知道是否有一种方法可以读取文件并将它们存储在一个单一的列表中,而不使用for循环?
以下是我的代码:
library(dplyr)
library(readxl)
library(XML)
# 获取所有文件名
files <- list.files(path="data", recursive=T, pattern="*.(csv|xls|xlsx)$", full.names=T)
### 以上导入的示例文件名
files <- c("data/Bills/BillHistory_Account1.csv", "data/Bills/BillHistory_2.xls", "data/Usages/UsageHistory_3.xls", "data/Usages/UsageHistory_4.xls.xlsx")
# 将每个数据框存储在一个列表中
df_list <- list()
for(i in seq_along(files)){
if(grepl("*.csv$", files[[i]])){
df_list[[i]] <- read.csv(files[[i]], sep="\t")
} else if(grepl("*.xls$", files[[i]])){
### 一些.xls文件实际上包含HTML代码
### 需要使用XML::readHTMLTable来读取
df_list[[i]] <- readHTMLTable(files[[i]])$tblMain
} else if(grepl("*.xlsx$", files[[i]])){
df_list[[i]] <- read_excel(files[[i]])
}
}
提前感谢您的帮助!
P.S. 我还想知道是否有一种方法可以创建一个可复制的示例来读取本地数据文件?我想不出其他方法,除非读者创建任意的本地CSV或Excel文件。
英文:
I have a list of 20+ data files with different file extensions (.csv, .xls, .xlsx). I want to store each data file as a data frame
in a list
. I wrote a for loop to do the task.
My "data" folder contains subfolders with different data files. Then within the loop, I called different functions accordingly depending on the file extension types. I was wondering if there is a way to read in files and store them in a single list without using a for loop?
Below is my code:
library(dplyr)
library(readxl)
library(XML)
# grab all file names
files <- list.files(path="data", recursive=T, pattern="*.(csv|xls|xlsx)$", full.names=T)
### example imported file names from above
files <- c("data/Bills/BillHistory_Account1.csv", "data/Bills/BillHistory_2.xls", "data/Usages/UsageHistory_3.xls", "data/Usages/UsageHistory_4.xls.xlsx")
# store each data frame in a list
df_list <- list()
for(i in seq_along(files)){
if(grepl("*.csv$", files[[i]])){
df_list[[i]] <- read.csv(files[[i]], sep="\t")
} else if(grepl("*.xls$", files[[i]])){
### some .xls files actually contain html codes
### need to read using XML::readHTMLTable
df_list[[i]] <- readHTMLTable(files[[i]])$tblMain
} else if(grepl("*.xlsx$", files[[i]])){
df_list[[i]] <- read_excel(files[[i]])
}
}
Thank you in advance for helping!
P.S. I was also wondering if there is a way to create a reproducible example for reading in local data files? I can't think of any means other than having the reader create arbitrary local csv, or excel files.
答案1
得分: 2
包{rio}是各种导入包的通用包装器。一个简单的import(filename.ext)
将猜测文件格式,因此整个任务可能会归结为:
# 获取所有文件名
files <- list.files(path="data", recursive=T, pattern="*.(csv|xls|xlsx)$", full.names=T)
dataframe_list <- Map(files, f = \(filename) rio::import(filename))
如果需要,仍然可以向import
添加特定于读取器的命名参数(sep
、header
、skip
...)。
英文:
Package {rio} is a universal wrapper around various import packages. A simple import(filename.ext)
will guess the file format, so the whole task might boil down to:
# grab all file names
files <- list.files(path="data", recursive=T, pattern="*.(csv|xls|xlsx)$", full.names=T)
dataframe_list <- Map(files, f = \(filename) rio::import(filename))
If needed, you can still add reader-specific named arguments (sep
, header
, skip
...) to import
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论