0
votes

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 ?

1
You are creating a matrix from a single value?. The append = TRUE should be correct rather than Trueakrun
Can you specify the package usedakrun
Package used is xlsx. Though the above is some random dataframe I created. I actually wanted to know how can I store each dataframe created inside a loop into an excel sheetJury Reta
Do you want to return the output as well?akrun
Please check the solution belowakrun

1 Answers

1
votes

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
  }