简体   繁体   English

循环读取和合并R中的多个Excel工作表

[英]Loop for Read and Merge multiple excel sheets in r

There are many posts about XLConnect and reading excel files in R with XLConnect like How to read multiple excel sheets in R programming? 关于XLConnect和使用XLConnect 读取 R中的excel文件的文章很多,例如如何在R编程中读取多个excel表? , including rbind function, but no one answers this question: ,包括rbind函数,但没有人回答这个问题:

If i had multiple excel .xls files in a directory how can i use a loop for reading and merging each one in order? 如果目录中有多个excel .xls文件,如何使用循环读取和合并每个循环?

I have a directory so i do this: 我有一个目录,所以我这样做:

 setwd("C:/Users/usuario/Desktop")
  library(rjava)
  library(XLConnect)

That directory has 28 excel files named like this: 该目录有28个excel文件,命名如下:

 Bitacora_Metrocali_01_02_2014C
 Bitacora_Metrocali_02_02_2014C
         .    ...    ...
 Bitacora_Metrocali_28_02_2014C

So i need to merge them using the function: Merge(x,y,all=T) 所以我需要使用以下功能合并它们:Merge(x,y,all = T)

So it can add new columns to the dataframe. 因此,它可以将新列添加到数据框。 The thig is, i need a dataframe that starts merging the first wht the second,and then adding all the new sheets in order. 想法是,我需要一个数据框,该数据框开始将第一个与第二个合并,然后按顺序添加所有新表。 All excel files of interest are in sheet 1. 所有感兴趣的Excel文件都在工作表1中。

THX! 谢谢!

Does this work for you: 这对您有用吗:

# This will give you a vector of the names of files in your current directory 
# (where I've assumed the directory contains only the files you want to read)
data.files = list.files()

# Read the first file
df = readWorksheetFromFile(file=data.files[1], sheet=1)

# Loop through the remaining files and merge them to the existing data frame
for (file in data.files[-1]) {
    newFile = readWorksheetFromFile(file=file, sheet=1)
    df = merge(df, newFile, all=TRUE)
}

Here's an lapply and Reduce approach I am using the the read.xls from gdata package, as you mentioned xls files. 正如您提到的xls文件一样,这是一种lapplyReduce方法,我正在使用gdata包中的read.xls。 If it is xlsx instead, substitute read.xls with readWorksheetFromFile and load the appropriate libraries. 如果不是xlsxread.xls readWorksheetFromFile替换为readWorksheetFromFile并加载适当的库。

library(gdata)
data.files = list.files(pattern = "*.xls") #get list of files
data.to.merge <- lapply(files, read.xls) #read in files using lapply
merged.data <- Reduce(function(...) merge(..., all = T),data.to.merge)#merge all the files

The merged.data will have data from all the sheets, and will also handle the case of files with different headers. merged.data将具有来自所有工作表的数据,还将处理具有不同标题的文件的情况。

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

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