DeaR all,

I'd like to know how to create an Excel file with multiple spreadsheets from
R. I searched the help files and found [1] but it is not what I want to do.

Let's say I have a data frame called DF as in the following code

# Some data
set.seed(123)
ID<-sample(5,100,replace=TRUE)
X<-rnorm(100)
Y<-rpois(100,10)
Z<-X*Y
DF<-data.frame(ID,X,Y,Z)
str(DF)

I'd like to split DF by ID and then save the result in an Excel file (say
"information.xls") with multiple spreadsheets (one per ID). Also, my output
should include the original data as a new spreadsheet called, for example,
"original". At the end I will have (in this example) six different
spreadsheets in "information.xls".

To split() DF I can easily do

# Splitting data
sDF<-with(DF,split(DF,ID))
sDF

and to save the information per ID in different Excel files, rather than in
"information.xls" with a spreadsheet by ID, I can do


# Working directory  -- please change this when run the code in your R
session
setwd("C:\\")

# Saving the information by ID in a different Excel file
# called "information_ID_numberhere.xls"
lapply(1:5,function(i)

write.table(sDF[[i]][,-1],paste("information_ID_",i,".xls",sep=""),col.names=TRUE,row.names=FALSE,sep="\t")
     )

How can I export DF (the original data), DF such as ID==1, DF such as
ID==2,..., DF such as ID==5 to "information.xls" (just one file) naming the
spreadsheets as "original", "ID_1", "ID_2",...,"ID_5", respectively?

I'm running R 2.8.1 Patched on XP. Here is my sessionInfo()

R version 2.8.1 Patched (2009-01-01 r47434)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] grid      splines   stats     graphics  grDevices utils     datasets
 methods   base

other attached packages:
 [1] ggplot2_0.8.2      reshape_0.8.2      plyr_0.1.5         proto_0.3-8
     epicalc_2.8.1.1
 [6] survival_2.34-1    foreign_0.8-33     lattice_0.17-20
 xlsReadWrite_1.3.2 RODBC_1.2-5


Any help would be greatly appreciated.

Thanks in advance,


Jorge Ivan Velez


[1] http://tinyurl.com/aurprr

	[[alternative HTML version deleted]]


