首页 > 解决方案 > 循环遍历标识符列表以加载相应的 excel 文件?

问题描述

我可以将一列唯一标识符作为对象导入 R。每个标识符都有一个匹配名称的 excel 文件(大约 500 个)。我正在尝试编写一个循环来遍历所有这些唯一 ID 并加载相应的 excel。

我尝试的是:

for (i in 1:nrow(pi)){
read_excel()
}

更新:所以只是为了澄清,因为我认为我没有提供足够的例子。

我有一个 excel 列,其中包含大约 500 个唯一 ID,每个 ID 都是一系列 11 个左右的数字。对于每个 ID,我都有一个名称匹配的 excel 文件。所有的excel文件都在同一个文件夹中。对于每个唯一 ID,我想打开具有匹配名称的文件,并检索特定的单元格,即特定列中的底部和顶部值、另一列的最大值或平均值等。

其中“pi”是应该是唯一 ID 向量的对象。我不知道如何完成这个。欢迎使用其他解决此问题的方法。实际上,我只是想从 excel 中检索特定值,即某一列中的第一个和最后一个值、另一列的最大值和平均值等。

标签: rexcel

解决方案


由于原始帖子没有提供数据,我将说明一种技术,我们使用 id 数字向量生成文件名,以读取与Pokémon第 1 至 8 代基本神奇宝贝统计数据相关的多个电子表格。

为了使示例完全可重现,我在 GitHub 上维护了一个包含此数据的 zip 文件,我们可以将其下载并加载到 R 中。

我们将使用该sprintf()函数来创建文件名,因为sprintf()它不仅允许我们添加定位文件所需的目录信息,还可以使用前导零格式化数字,这是生成正确文件名所必需的。

for()我们将使用lapply()匿名函数代替循环来创建文件名并将它们作为 Excel 文件读取readxl::read_excel()

download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/PokemonXLSX.zip",
               "PokemonXLSX.zip",
               method="curl",mode="wb")
unzip("PokemonXLSX.zip",exdir="./pokemonData")
library(readxl)
# create a set of numbers to be used to generate 
generationIds <- 1:8
spreadsheets <- lapply(generationIds,function(x) {
     # use generation number to create individual file name
     aFile <- sprintf("./PokemonData/gen%02i.xlsx",x)
     data <- read_excel(aFile)
     })

此时对象spreadsheets是一个包含八个元素的列表,一个对应于每一代神奇宝贝(即每个电子表格一个元素)。

我们可以将这七个文件与 结合起来rbind(),然后打印结果数据帧的最后几行。

pokemon <- do.call(rbind,spreadsheets)
tail(pokemon)

...结果:

> tail(pokemon)
# A tibble: 6 x 13
     ID Name  Form  Type1 Type2 Total    HP Attack Defense Sp..Atk Sp..Def
  <dbl> <chr> <chr> <chr> <chr> <dbl> <dbl>  <dbl>   <dbl>   <dbl>   <dbl>
1   895 Regi… NA    Drag… NA      580   200    100      50     100      50
2   896 Glas… NA    Ice   NA      580   100    145     130      65     110
3   897 Spec… NA    Ghost NA      580   100     65      60     145      80
4   898 Caly… NA    Psyc… Grass   500   100     80      80      80      80
5   898 Caly… Ice … Psyc… Ice     680   100    165     150      85     130
6   898 Caly… Shad… Psyc… Ghost   680   100     85      80     165     100
# … with 2 more variables: Speed <dbl>, Generation <dbl>

聚焦:从磁盘访问文件

为了隔离下载的文件,我们使用exdir=参数 onunzip()将解压缩的文件写入 R 工作目录的子目录。

我们可以通过添加./pokemonData/文件名来访问此子目录中的文件。此.语法中的 引用当前目录。

我们可以说明如何使用以下代码创建文件名。

theFiles <- lapply(generationIds,function(x) {
        # use generation number to create individual file name
        aFile <- sprintf("./pokemonData/gen%02i.xlsx",x)
        message(paste("current file is: ",aFile))
        aFile
}) 

...和输出:

> theFiles <- lapply(generationIds,function(x) {
+         # use generation number to create individual file name
+         aFile <- sprintf("./pokemonData/gen%02i.xlsx",x)
+         message(paste("current file is: ",aFile))
+         aFile
+ })
current file is:  ./pokemonData/gen01.xlsx
current file is:  ./pokemonData/gen02.xlsx
current file is:  ./pokemonData/gen03.xlsx
current file is:  ./pokemonData/gen04.xlsx
current file is:  ./pokemonData/gen05.xlsx
current file is:  ./pokemonData/gen06.xlsx
current file is:  ./pokemonData/gen07.xlsx
current file is:  ./pokemonData/gen08.xlsx

可以使用该功能从 RStudio 中识别 R 工作目录getwd()。在我的 MacBook Pro 上,我得到以下结果。

> getwd()
[1] "/Users/lgreski/gitrepos/datascience"
>

推荐阅读