简体   繁体   中英

R XLConnect readWorksheet: rename column names in each worksheet

I am parsing an excel file with several worksheets and three columns in each worksheet. The three columns have slightly different names in each worksheet (DATE VS Date, etc), so when I execute my code the df data frame has several columns of data. I want to condense df to 3 columns by renaming the headers from each excel sheet. How can I rename the header values when I read in each worksheet?

require(XLConnect)
wb <- loadWorkbook("~/Downloads/BearRiverBand-Rancheria-WindTurbine-Log-2009-2014.xlsx")
lst = readWorksheet(wb, sheet = getSheets(wb))
df <- ldply (lst, data.frame)

I solved my problem:

require(XLConnect)
require(plyr)
wb <- loadWorkbook("~/Downloads/BearRiverBand-Rancheria-WindTurbine-Log-2009-2014.xlsx")
lst = readWorksheet(wb, sheet = getSheets(wb))

dat=data.frame()

for (l in 1:(length(lst)-4)){
  s <- data.frame(lst[l])
  names(s) <- c('TIME','DATA','BY')
  dat <- merge(dat,s,all = TRUE)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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