How to add data from data frame in R to an excel sheet that already exists

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

Here are the two pages that I looked at while trying to find the answer (I don't have 10 reps, so I can’t insert all four URLs of the webpage I visited):

Writing data to an Excel file using the xlsx R-package The author says: "You can also add dataframes to a specific starting location on the worksheet using the startRow and startCol arguments for 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 assumption, this was my attempt at RStudio:

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

This was the output of R: Error in the $ getWorkbook sheet: the $ operator is not valid for atomic vectors

I also tried this page:

Excel R " , :"

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)

, . Excel?

+4
1

Excel . Excel R, R, Excel ( csv, , Excel ). . , .

: R

, Excel , Excel , :

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)

:

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

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

: Excel

, Excel ( , , R). iris :

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

Excel, Excel , :

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

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

. startRow , .

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]])

  }

:

saveWorkbook(wb, "combinedData.xlsx")

addDataFrame , Excel . , , , R, Excel ( csv ) .

+7

Source: https://habr.com/ru/post/1623162/


All Articles