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

writing multiple data frames into the same excel file in R

first off I have below sample data frames:

a<-(1:10)
b<-(10:19)
c<-(1:10)
d<-(20:29)
df_t1<-data.frame(a,b)
df_t2<-data.frame(c,d)

And then I try to save the 2 data frames into one excel file:

Saving the first one was okay:

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

write.xlsx(df_t1,file="demo.xlsx",sheetName="sheet1",rowNames=FALSE)

enter image description here

However, when I ran the second line:

write.xlsx(df_t1,file="demo.xlsx",sheetName="sheet2",append=TRUE,rowNames=FALSE)

enter image description here

Instead of adding a new sheet named "sheet 2", it overwrites "sheet1" with "sheet2" even append is set to be TRUE in second line. Any ideas on what went wrong? Cheers.

>Solution :

I acheived it by using write.xlsx from openxlsx. I passed the two dataframes
to the method in a list. write.xlsx from xlsx seems to have issues lately.

a<-(1:10)
b<-(10:19)
c<-(1:10)
d<-(20:29)
df_t1<-data.frame(a,b)
df_t2<-data.frame(c,d)
listOfData <- list("sheet1"=df_t1,"sheet2"=df_t2)
openxlsx::write.xlsx(listOfData,file="demo.xlsx",rowNames=FALSE)
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