Jury Reta
Jury Reta

Reputation: 1

How to add each data frame created inside a loop to a separate sheet in excel?

for(x in unique_seg){

bf <- data.frame(matrix(x,nrow =3,ncol =3))

write.xlsx(bf,'bf.xlsx', sheetname =x, append=True) }

But I see only last loop is returned. How to get all sheets in 1 excel ?

Upvotes: 0

Views: 30

Answers (1)

akrun
akrun

Reputation: 886948

If we want to return the output, create a list and assign the output to the list

lst1 <- vector('list', length(unique_seg))
names(lst1) <- unique_seg    

for(x in unique_seg){

   bf <- data.frame(matrix(x,nrow =3,ncol =3))

  write.xlsx(bf,'bf.xlsx', sheetname =x, append=TRUE)
  lst1[[x]] <- bf
  }

Upvotes: 1

Related Questions