我有几个(70 多个)工作表的工作簿。每个工作表都是一个站名。每个工作表都有不同数量的注释行,以我要删除的 # 开头,然后从注释结束时读取标题并将它们分配给列名。我试图使用在 32 位机器/R/EXCEL 上工作但在 64 位系统上不工作的 RODBC。以下是我使用 RODBC 的内容。对于 64 位系统,我如何使用 XLconnect 或其他东西来做同样的事情?
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()