read.xls - read in variable-length list of sheets, with their names

前端 未结 2 703
情歌与酒
情歌与酒 2021-01-18 07:55

Given several .xls files with varying number of sheets, I am reading them into R usingread.xls from the gdata package. I

2条回答
  •  爱一瞬间的悲伤
    2021-01-18 08:36

    For such tasks I use library XLConnect. With its functions you can get the names of each sheet in a vector and then just determine the length of that vector.

    #Read your workbook 
    wb<-loadWorkbook("Your_workbook.xls")
    
    #Save each sheet's name as a vector
    lp<-getSheets(wb)
    
    #Now read each sheet as separate list element
    dat<-lapply(seq_along(lp),function(i) readWorksheet(wb,sheet=lp[i]))
    

    UPDATE

    As suggested by @Martin Studer XLConnect functions are already vectorized, so there is no need to use lapply(), instead just provide vector of sheet names or use function getSheets() inside readWorksheet().

    dat <- readWorksheet(wb, sheet = getSheets(wb))
    

提交回复
热议问题