【问题标题】:Writing for loop to read excel sheets编写for循环以读取excel表
【发布时间】:2021-03-22 15:44:01
【问题描述】:

我需要将多张工作表从 excel 读入 R(读入单独的数据框)。我不知道如何编写 for 循环,所以我只是使用了蛮力。

这是我的代码

mydata1 <- read_excel("exceldata.xlsx", sheet = 1)
mydata2 <- read_excel("exceldata.xlsx", sheet = 2)
mydata3 <- read_excel("exceldata.xlsx", sheet = 3)
mydata4 <- read_excel("exceldata.xlsx", sheet = 4)
mydata5 <- read_excel("exceldata.xlsx", sheet = 5)
mydata6 <- read_excel("exceldata.xlsx", sheet = 6)
mydata7 <- read_excel("exceldata.xlsx", sheet = 7)
mydata8 <- read_excel("exceldata.xlsx", sheet = 8)
mydata9 <- read_excel("exceldata.xlsx", sheet = 9)

这可行,但我希望有人能告诉我如何使用 for 循环或 lapply 来代替。我还需要上传数据文件吗?我是这个网站的新手。

谢谢。

【问题讨论】:

  • mydata &lt;- lapply(1:9, read_excel, path="exceldata.xlsx") 会将工作表读入列表。如果你想命名元素,那么mydata &lt;- sapply(sheet_names(path), read_excel, path=path, simplify = FALSE).

标签: r excel dataframe readxl


【解决方案1】:

我们可以在工作表编号索引上使用循环并将它们读入list

mylist <- lapply(1:9, function(i) read_excel("excel_data.xlsx", sheet = i))

最好将其保存在list 中,而不是在全局环境中创建多个对象。 list 也可以命名

names(mylist) <- paste0('mydata', seq_along(mylist))

可以使用[[$提取元素

mylist[["mydata1"]]
mylist$mydata2

使用for循环,可以先初始化一个list

mylist2 <- vector('list', 9)
for(i in seq_along(mylist2)) {
    mylist2[[i]] <- read_excel("excel_data.xlsx", sheet = i)
 }

【讨论】:

  • 另外,如果您有许多 excel 文件并且它们并不总是具有相同的工作表,readxl::excel_sheets() 将返回一个带有工作表名称的字符向量。因此,您可以使用更通用的东西来代替lapply(1:9, ...),例如lapply(excel_sheets("excel_data.xlsx"), ...)
【解决方案2】:

这个问题out there有很多answers

无论如何,对于刚接触 R 的人来说,一个简单的解决方案可能是:

# load library
library("openxlsx")

# set path to the directory containing the files
myDir <- "path/to/files/"

# read all the files in the directory
fileNames <- list.files(myDir)

# declare empty list
allFiles <- list()

# set counting index
i <- 1

# loop through the files in myDir
for (fileN in fileNames) {
    # read the file and store in position i
    allFiles[[fileN]] <- read.xlsx(paste(myDir, fileN, sep=""))
    # go to next position by updating the counting index
    i <- i + 1
}
# at this point you can access your files by, for instance:
allFiles[[1]]
# where 1 is the first file in your file list

或者,如果您不想使用计数索引:

# load library
library("openxlsx")

# set path to the directory containing the files
myDir <- "path/to/files/"

# read all the files in the directory
fileNames <- list.files(myDir)

# declare empty list
allFiles <- list()

# loop through the files in myDir
for (fileN in fileNames) {
    # read the file and store it into the list
    allFiles[[fileN]] <- read.xlsx(paste(myDir, fileN, sep=""))
}
# at this point you can access your files by, for instance:
allFiles[[fileN]]
# where fileN is the variable you just used in the for loop
# or you can access them using the actual file names but with quotes
allFiles[["actual_file_name"]]

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 2019-03-11
    • 2020-09-27
    • 2013-02-16
    • 1970-01-01
    相关资源
    最近更新 更多