0

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 ?

7
  • You are creating a matrix from a single value?. The append = TRUE should be correct rather than True Commented Dec 11, 2020 at 18:52
  • Can you specify the package used Commented Dec 11, 2020 at 18:53
  • 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 sheet Commented Dec 11, 2020 at 18:59
  • Do you want to return the output as well? Commented Dec 11, 2020 at 18:59
  • Please check the solution below Commented Dec 11, 2020 at 19:01

1 Answer 1

1

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
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.