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?