【问题标题】:How to read multiple xlsx file in R using loop with specific rows and columns如何使用具有特定行和列的循环在 R 中读取多个 xlsx 文件
【发布时间】:2015-11-19 03:34:18
【问题描述】:

我必须将具有随机名称的多个 xlsx 文件读入单个数据帧。每个文件的结构都是一样的。我只需要导入特定的列。

我试过了:

dat <- read.xlsx("FILE.xlsx", sheetIndex=1, 
                  sheetName=NULL, startRow=5, 
                  endRow=NULL, as.data.frame=TRUE, 
                  header=TRUE)

但这一次只针对一个文件,我无法指定我的特定列。 我什至尝试过:

site=list.files(pattern='[.]xls')

但在那之后循环不起作用。怎么做?提前致谢。

【问题讨论】:

    标签: r excel loops dataframe xlsx


    【解决方案1】:

    我会把每张纸读成一个列表:

    获取文件名:

    f = list.files("./")
    

    读取文件:

    dat = lapply(f, function(i){
        x = read.xlsx(i, sheetIndex=1, sheetName=NULL, startRow=5,
            endRow=NULL, as.data.frame=TRUE, header=T)
        # Get the columns you want, e.g. 1, 3, 5
        x = x[, c(1, 3, 5)]
        # You may want to add a column to say which file they're from
        x$file = i
        # Return your data
        x
    })
    

    然后您可以通过以下方式访问列表中的项目:

    dat[[1]]
    

    或者对他们做同样的任务:

    lapply(dat, colmeans)
    

    将它们变成一个数据框(您的文件列现在在这里变得有用):

    dat = do.call("rbind.data.frame", dat)
    

    【讨论】:

    • 嘿,你的代码确实对我有用,但它只适用于非常小的文件,比如 50KB。但我有大约 1MB 大小的文件。那我该怎么办?
    • 尝试不同的excel阅读包,这个可能是你最好的选择:blog.rstudio.org/2015/04/15/readxl-0-1-0。另请注意,同时打开多个文件可能会导致您出现内存问题。
    • 我正在尝试做类似的事情,但在最终数据帧中获得 0 obs path = "/K_Data/" l = list.files(path = path, "xlsx") all = lapply(l, function(x){ sup = read_excel(i, sheet="Sup", skip = 2) ID = read_excel(x, sheet="Mea", col_names = FALSE) id = as.character( ID[1,1]) mass = as.numeric(ID[3,5]) sup= sup%&gt;% mutate(ID = id, Mass = mass) }) dat = do.call("rbind.data.frame", all)
    • 您需要在 lapply 函数结束时打印/返回您的数据。例如:... sup= sup%>% mutate(ID = id, Mass = mass) sup}) dat = do.call("rbind.data.frame", all)
    【解决方案2】:

    我更熟悉 for 循环,它可能会更麻烦一些。

    filelist &lt;- list.files(pattern = "\\.xlsx") # 列出目录下所有的xlsx文件

    allxlsx.files <- list()  # create a list to populate with xlsx data (if you wind to bind all the rows together)
    count <- 1
    for (file in filelist) {
       dat <- read.xlsx(file, sheetIndex=1, 
                  sheetName=NULL, startRow=5, 
                  endRow=NULL, as.data.frame=TRUE, 
                  header=TRUE) [c(5:10, 12,15)] # index your columns of interest
       allxlsx.files[[count]] <-dat # creat a list of rows from xls files
       count <- count + 1
    }
    

    转换回data.frame

    allfiles <- do.call(rbind.data.frame, allxlsx.files)
    

    【讨论】:

      【解决方案3】:

      对于 Wyldsoul 的答案的变体,但在同一个 Excel 文件中跨多个 Excel 工作表(1 和 j 之间)使用 for 循环,并与 dplyr 绑定:

      library(gdata) 
      library(dplyr)
      
      for (i in 1:j) {
        dat <- read.xls(f, sheet = i) 
        dat <- dat[,1:14] # index your columns of interest
        allxlsx.files[[count]]
        count <- count + 1
      }
      
      allfiles <- do.call(bind_rows, allxlsx.files)
      

      【讨论】:

      • 你需要分配一些东西给allxlsx.files[[count]],可能是dat,你可以将你的最后一行简化为bind_rows(allxlsx.files)
      猜你喜欢
      • 1970-01-01
      • 2020-02-05
      • 1970-01-01
      • 2018-02-15
      • 2016-11-21
      • 2015-04-23
      • 2021-08-04
      • 2021-06-24
      • 1970-01-01
      相关资源
      最近更新 更多