Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用具有特定行和列的循环读取R中的多个xlsx文件_R_Excel_Loops_Dataframe_Xlsx - Fatal编程技术网

如何使用具有特定行和列的循环读取R中的多个xlsx文件

如何使用具有特定行和列的循环读取R中的多个xlsx文件,r,excel,loops,dataframe,xlsx,R,Excel,Loops,Dataframe,Xlsx,我必须将多个带有随机名称的xlsx文件读入单个数据帧。每个文件的结构都是相同的。我必须只导入特定列 我试过这个: dat <- read.xlsx("FILE.xlsx", sheetIndex=1, sheetName=NULL, startRow=5, endRow=NULL, as.data.frame=TRUE, header=TRUE) 但在那之后,循环就不起作用了

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

我试过这个:

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

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

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

获取文件名:

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)
将它们转换为数据帧(此时文件列将变得有用):


我更熟悉for循环,它可能有点麻烦


filelist了解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)
库(gdata)
图书馆(dplyr)
对于(1:j中的i){

dat Hey你的代码确实对我有用,但它只对非常小的文件(例如50KB)有效。但我有大约1MB大小的文件。我该怎么办?尝试其他excel阅读包,这可能是你的最佳选择:。还要注意,同时打开多个文件可能会导致内存问题。我正在尝试类似的操作ar但在最终数据帧中获取0 obs
path=“/K_Data/”l=list.files(path=path,“xlsx”)all=lappy(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%>%mutate(ID=ID,mass=mass)})dat=do.call(“rbind.data.frame”,all)
您需要在lappy函数末尾打印/返回数据。例如:…sup=sup%>%mutate(ID=ID,mass=mass)sup})dat=do.call(“rbind.data.frame”,all)您需要为
allxlsx.files[[count]]
,可能是
dat
,您可以将最后一行简化为
bind_行(allxlsx.files)
dat = do.call("rbind.data.frame", dat)
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
}
allfiles <- do.call(rbind.data.frame, allxlsx.files)
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)