从工作簿中的多个工作表中读取数据并消除注释

时间:2014-05-01 18:00:34

标签: r rodbc xlconnect

我有几本(70+)工作表的工作簿。每个工作表都是一个工作站名称。每个工作表都有不同数量的注释行,以#开头,我想消除,然后从注释结束时读取标题并将它们分配给列名。我试图使用RODBC,它可以在32位机器/ R / EXCEL上运行,但不能在64位系统上运行。以下是我使用RODBC的内容。如何使用XLconnect或其他方式为64位系统执行相同的操作?

library(RODBC)
### Connect to Excel Files
# Input
excel.input <- odbcConnectExcel2007("Diurnal Data 2013.xlsx")
# Output
excel.output <- odbcConnectExcel("Diurnal Data 2013 edit.xls", readOnly=FALSE)
### Pull and Format Data
#extract and keep Stations sheet
Stations <-sqlFetch(excel.input, "Stations", na.strings=c("","-")) 
sqlSave(excel.output,Stations,  rownames = FALSE)
##Loop through data pull and formatting for each site id
sites <- gsub("[[:punct:]]","",sqlTables(excel.input)[,"TABLE_NAME"])
len <- length(sites)
for(i in 2:len) { # Omit the 1st worksheet
sheet <- sites[i]
data <- sqlFetch(excel.input, sheet, na.strings=c("","-")) ##retrieve data from each worksheet   ith sheet
data1 <- subset(data, !grepl("^#", data[,1])) ## Removes rows starting  with #(hashtag) on ith sheet
# Rename to appropriate column headers
A <- t(data1[1,])
names(data1)<- A[,1] 
# Remove unwanted rows
data2 <- data1[c(-1,-2),]
head(data2)
# Write new ith worksheet into output file.
sqlSave(excel.output,data2, tablename=sheet, rownames = FALSE)
# End loop after running through all sheets.
}
###Close connections to Excel files before opening
odbcCloseAll()

0 个答案:

没有答案