I'm attempting to write an R script that will loop through about 100 small Excel spreadsheets and stack the 15-20 rows that have full cells (the exact number of rows varies in each small file) into one.
This code that I'm using (contained within the loop) reads the files in, but the columns that do not contain any entries are skipped.
newDf <- read.xlsx(filename,sheetName="Coding",header=FALSE,
rowIndex=rows,colIndex=columns)
My current workaround is to create a dummy first dataframe with values in every column; when I then rbind each newDf to it, the columns are forced to line up. But I'm hoping there's a better way.
Any help would be appreciated. Thank you.
If you're willing to use the
XLConnect
package rather thanxlsx
, the functionXLConnect::readWorksheet
allows you to specify thestartCol
,endCol
, andautofitCol
arguments, which would solve this issue for you:where
df1
was read from a worksheet containing only two non-empty columns (see data below).Data: