Trying to read excel data from a worksheet and split that based on different logic into to multiple dataframes. These multiple dataframes need to be written to the same excel sheet in different worksheets using R. Kindly suggest packages or sample code where these packages were used.
Asked
Active
Viewed 637 times
0
-
1Have a [look](http://stackoverflow.com/documentation/r/5536/i-o-for-foreign-tables-excel-sas-spss-stata/4445/importing-excel-files#t=201702070710364373435) – user2100721 Feb 07 '17 at 07:12
1 Answers
0
To read from an excel file, read this post: Read an Excel file directly from a R script
To create a multiple worksheet excel file, the package xlsx may be of help. The following function takes a filename, a vector of worksheet names, and the data frame object names. Note to provide an equal sized vector to the data frame object arguments.
save2.xlsx <- function (file, namelist, ...)
{
require(xlsx, quietly = TRUE)
objects <- list(...)
fargs <- as.list(match.call(expand.dots = TRUE))
objnames <- as.character(fargs)[-c(1, 2)]
nobjects <- length(objects)
for (i in 1:nobjects) {
if (i == 1)
write.xlsx(objects[[i]], file, sheetName = namelist[i])
else write.xlsx(objects[[i]], file, sheetName = namelist[i],
append = TRUE)
}
}
Example call:
mydf1<- data.frame(matrix(data=rnorm(4),ncol=2,nrow=2))
mydf2<- data.frame(matrix(data=rnorm(9),ncol=3,nrow=3))
save2.xlsx("C:\\myfile.xls",c("Mysheet1", "Mysheet2"),mydf1, mydf2 )