[R] write.xlsx- writing in a single sheet

Jim Lemon drjimlemon at gmail.com
Fri Jan 15 21:41:31 CET 2016


Hi Mohsen,
I can guess two things that you might want. One is to join all of the data
in the "CSV" files into a single data frame and then write that to a single
XLSX sheet. If the names and data structure of these files are similar
enough to "rbind" them, just do this in R and then write the result.

If the names are different, you can change the names to be consistent
across the data frames or reorder them.

If the structures are different, add columns of NAs where the numbers of
columns are different and "rbind" or "merge".

Jim

On Sat, Jan 16, 2016 at 1:43 AM, Mohsen Jafarikia <jafarikia at gmail.com>
wrote:

> Hello all:
>
> I am having problem writing a few files in a single sheet of excel. It
> seems R has problem writing on the same sheet. Maybe there is a command
> that I am missing. Here is the code I am using:
>
> library(xlsx)
>
> ifn11 <- "A1.xlsx"
> dat11  <- read.xlsx(ifn11, sheetName="A.csv", header = TRUE)
>
> ifn12 <- "A2.xlsx"
> dat12  <- read.xlsx(ifn12, sheetName="A.csv", header = TRUE)
>
> ifn13 <- "A3.xlsx"
> dat13  <- read.xlsx(ifn13, sheetName="A.csv", header = TRUE)
>
> ifn21 <- "F1.xlsx"
> dat21 <- read.xlsx(ifn21, sheetName="F.csv",header = TRUE)
>
> ifn22 <- "F2.xlsx"
> dat22  <- read.xlsx(ifn22, sheetName="F.csv",header = TRUE)
>
> ifn23 <- "F3.xlsx"
> dat23 <- read.xlsx(ifn23, sheetName="F.csv",header = TRUE)
>
> write.xlsx(dat11,  file="AC.xlsx", sheetName="A",  append=FALSE)
> write.xlsx(dat12,  file="AC.xlsx",                             append=
> TRUE)
> write.xlsx(dat13,  file="AC.xlsx",                             append=
> TRUE)
> write.xlsx(dat21,  file="AC.xlsx", sheetName="F",  append= TRUE)
> write.xlsx(dat22,  file="AC.xlsx",                             append=
> TRUE)
> write.xlsx(dat23,  file="AC.xlsx",                             append=
> TRUE)
>
> And here is the error message I am having:
>
> Error in .jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet",
>  :
>   java.lang.IllegalArgumentException: The workbook already contains a sheet
> of this name
>
> This error message comes after running the write.xlsx(dat13,
>  file="AC.xlsx", showNA=FALSE, row.names=FALSE, append= TRUE) line. Program
> creates a sheet named "A" when writes dat11, then creates "sheet1" after
> writing dat12 and when tries to write dat13, it gives me error. It seems it
> tries to write on "sheet1" which already exists. I would like dat11, dat12
> and dat13 will be all written after each other on sheet "A" and dat12,
> dat22 and dat23 in sheet "F".
>
> Anybody has any comments please.
>
> Regards,
> Mohsen
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list