Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alignment Issue with write.xlsx in R

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

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

Does anyone know the way around this?

Thanks!

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading