简体   繁体   English

读取多个“.xlsx”文件

[英]Read multiple “.xlsx” files

I am trying to read multiple excel files under different folders by R我正在尝试通过 R 读取不同文件夹下的多个 excel 文件

Here is my solution:这是我的解决方案:

    setwd("D:/data")
    filename <- list.files(getwd(),full.names = TRUE) 
# Four folders "epdata1" "epdata2" "epdata3" "epdata4" were inside the folder "data" 
    dataname <- list.files(filename,pattern="*.xlsx$",full.names = TRUE)
# Every folder in the folder "data" contains five excel files
    datalist <- lapply(dataname,read_xlsx)

    Error: `path` does not exist:'D:/data/epidata1/出院舱随访1.xlsx'

But read_xlsx was successfully run但是 read_xlsx 运行成功

   read_xlsx("D:/data/epidata1/出院舱随访1.xlsx")

All file directories are available in the "data" folder and why R fails to read those excel file?所有文件目录都在“数据”文件夹中可用,为什么 R 无法读取这些 excel 文件?

Your help will much appreciated!您的帮助将不胜感激!

I dont see any point why your code shouldnt work.我看不出为什么你的代码不应该工作。 Make sure your folder names are correct.确保您的文件夹名称正确。 In your comments you write "epdata1" and your error says "epidata1".在你的评论中你写“epdata1”,你的错误说“epidata1”。 I tried it with some csv and mixed xlsx files.我尝试了一些 csv 和混合的 xlsx 文件。

This is again what i would come up with, to find the error/typo:这又是我想找到的错误/错字:

library(readxl)

pp <- function(...){print(paste(...))}


main <- function(){
  # finding / setting up data main folder
  # You may change this to your needs
main_dir <- paste0(getwd(),"/data/")

pp("working directory:",dir_data)
pp("Found following folders:")
pp(list.files(main_dir,full.names = FALSE))
data_folders <- list.files(dir_data,full.names = TRUE)

pp("Found these files in folders:",list.files(data_folders,full.names = TRUE))
pp("Filtering *.xlsx files",list.files(data_folders,pattern="*.xlsx$",full.names = TRUE))
files <- list.files(data_folders,pattern="\\.xlsx$",full.names = TRUE)

datalist <- lapply(files,read_xlsx)

print(datalist)
}

main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM