Home » excel » How do I append data from a data frame in R to an Excel sheet that already exists

How do I append data from a data frame in R to an Excel sheet that already exists

Posted by: admin March 9, 2020 Leave a comment

Questions:

I have created dozens of data frames in R and would like to append them all to one sheet in an Excel file.

Here are two of the pages I have looked at in an attempt to find an answer (I don’t have 10 reputations so I can’t paste all four webpage urls I have visited):

Write data to Excel file using R package xlsx
The author says: “You can also add the dataframes to a particular starting place in the sheet using the startRow and startCol arguments to the addDataFrame function.”
Here is the suggested code:

workbook.sheets workbook.test addDataFrame(x = sample.dataframe, sheet = workbook.test,
   row.names = FALSE, startColumn = 4) # write data to sheet starting on line 1, column 4
saveWorkbook(workbook.sheets, "test.excelfile.xlsx") # and of course you need to save it.

Based on this suggestion, this was my attempt in RStudio:

addDataFrame(df_fl1, sheet = "AllData2.xlsx", startRow = 712)

This was R’s output:
Error in sheet$getWorkbook : $ operator is invalid for atomic vectors

I’ve also tried this page:

Tutorial on Reading and Importing Excel Files into R
“If, however, you want to write the data frame to a file that already exists, you can execute the following command:”

write.xlsx(df, 
           "<name and extension of your existing file>", 
           sheetName="Data Frame"
           append=TRUE)
write.xlsx(df_fl3, "AllData2.xlsx", sheetName="Salinity1", append=TRUE)

I tried this code and it overwrote the data that was already in the sheet. How can I append data from the data frames into an Excel sheet?

How to&Answers:

Appending to an existing Excel worksheet is a bit of a pain. Instead, read all of your Excel data files into R, combine them within R, and then write the single combined data frame to a new Excel file (or write to a csv file if you don’t need the data to be in an Excel workbook). See code below for both the easy way and the hard way.

Easy Way: Do all the work in R and save a single combined data frame at the end

For example, if all of your Excel data files are in the current working directory and the first worksheet in each Excel file contains the data, you could do the following:

library(xlsx)

# Get file names
file.names = list.files(pattern="xlsx$")

# Read them into a list
df.list = lapply(file.names, read.xlsx, sheetIndex=1, header=TRUE)

Then combine them into a single data frame and write to disk:

df = do.call(rbind, df.list)

write.xlsx(df, "combinedData.xlsx", sheetName="data", row.names=FALSE)

Hard Way: Append successive data frames to a pre-existing Excel worksheet

Create a list of data frames that we want to write to Excel (as discussed above, in your actual use case, you’ll read your data files into a list in R). We’ll use the built-in iris data frame for illustration here:

df.list = split(iris, iris$Species)

To write each data frame to a single Excel worksheet, first, create an Excel workbook and the worksheet where we want to write the data:

wb = createWorkbook()
sheet = createSheet(wb, "data")

# Add the first data frame
addDataFrame(df.list[[1]], sheet=sheet, row.names=FALSE, startRow=1)

Now append all of the remaining data frames using a loop. Increment startRow each time so that the next data frame is written in the correct location.

startRow = nrow(df.list[[1]]) + 2    

for (i in 2:length(df.list)) {

  addDataFrame(df.list[[i]], sheet=sheet, row.names=FALSE, col.names=FALSE, 
               startRow=startRow)

  startRow = startRow + nrow(df.list[[i]])

  }

Save the workbook:

saveWorkbook(wb, "combinedData.xlsx")

addDataFrame is useful if you want to layout various summary tables in various parts of an Excel worksheet and make it all look nice for presentation. However, if you’re just combining raw data into a single data file, I think it’s a lot easier to do all the work in R and then just write the combined data frame to an Excel worksheet (or csv file) at the end.

Answer:

To get around the original error that you mentioned:

Error in sheet$getWorkbook : $ operator is invalid for atomic vectors

You can try this:

wb <- loadWorkbook("<name and extension of your existing file>")
addDataFrame(df,getSheets(wb)$<sheetname>, startRow = 712)
saveWorkbook(wb, <filename>)