Home > database >  Alignment Issue with write.xlsx in R
Alignment Issue with write.xlsx in R

Time:08-01

I'm trying to write a list of data frames into a single excel sheet. However, I'm having issues with the alignments with the data frames printed on the excel sheet.

While the first few data frame was fine, there are a few which was forced onto a new row which could make it harder to read.

I'm using the write.xlsx function

df = list("df1" = df1, "df2" = df2 ... )

write.xlsx(z = df, file = "list_of_df.xlsx")

Does anyone know the way around this?

Thanks!

CodePudding user response:

Based on your code, it seems that you're trying to output the dataframes into the same sheet. In this case, write.xlsx will take the length of the first dataframe - df1 as the benchmark on the maximum number of columns.

A quick fix would be to introduce a "ghost" dataframe to "trick" write.xlsx into expending your maximum number of columns. Hope this helps!

index = as.data.frame.matrix(t(c(1:80))) # I'm setting the maximum number of column to 80 here for illustration

df = list("index" = index, "df1" = df1, "df2" = df2 ... )
write.xlsx(z = df, file = "list_of_df.xlsx")
  • Related