Export Multiple Data Frames to Different Excel Worksheets in R (2 Examples) | write.xlsx2 & for-Loop

preview_player
Показать описание
R code of this video:

y = 1:5)
x2 = 9:5,
x3 = 7)
y = letters[1:5])

library("xlsx")

my_path <- "C:/Users/Joach/Desktop/my_directory/" # Store directory path

data_names <- c("data1", "data2", "data3") # Vector of data names

for(i in 2:length(data_names)) {
}

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Nice tutorial Joachim! Thank you for sharing this useful feature!

vijayarjunwadkar
Автор

In live with it. Will apply a lot of knowledge this week

ahmedJaber
Автор

Thank you a lot for sharing this!!, it was really helpful for me

danielechavezs
Автор

Thank you Sir.
I have a problem with the code cause when I save the document it doesn't appear the others sheets.

rafaellagunas
Автор

This is an extremely useful video, however, one key point missing in the code, that would have made this video absolutely perfect is automating the addition of data_names, as there can be instances where there are a huge number of data frames and are dynamic as well. Nevertheless, you have been extremely helpful with this video Joachim!! Keep sharing :)

anandmishra
Автор

THis is a great video. I cannot install XLSX package! could you please help me through this problem?

studyforfun
Автор

Excellent sir !. Is it possible to create many data to single excel
sheet?

varunkumar-bdnl
Автор

Thank you Sir for the materials you have shared in this video,

I have a question in exporting the data from R to excel, I am running the code correctly but i can't export the data, see the error message I am getting
> write.xlsx2(nov_dec, paste0(my_path, "data_all.xlsx"), row.names=FALSE, sheetName="nov_dec")
Error in write.xlsx2(nov_dec, paste0(my_path, "data_all.xlsx"), row.names = FALSE, :
could not find function "write.xlsx2"
Here are my codes

install.packages("xlsx")
library("xlsx")

my_path <- "C:/Users/vrwema/Desktop/R"
write.xlsx2(nov_dec, paste0(my_path, "data_all.xlsx"), row.names=FALSE, sheetName="nov_dec")

Kindly i need your help
thanks

valensrwema
Автор

Question about this:

I always set up my work directory before I really start working.


I attempted to use the

getwd() function to get my pathing and set up the pathing. When I did that it stored it on my desktop instead of in the work directory.

I have provided sample code below:

excel_path <- #I changed the name of the actual path for obvious reasons

write.xlsx2(Merged_ABP1_DF, paste0(excel_path, "Merged_Data.xlsx"), row.names= TRUE, sheetName = "sheet_1")

It also used the pathing toafter the last / as part of the excel name so it became:




However when I removed the pathing it saved appropriately? Why would this be. Perhaps this can help others who run into the same issue. I provided the new version below:

write.xlsx2(Merged_ABP1_DF, paste0( "Merged_Data_path.xlsx"), row.names= TRUE, sheetName = "sheet_1") #path wasn't needed because we are already in the work directory


you can ignore the comments I usually comment almost every line of code or every cluster of code for my self.

vanhoot