###################################
####Writing data into excel function#########
###################################
#Method one
# Write the first data set in a new workbook
write.xlsx(Data1, file=”exportedata.xlsx”,sheetName=”USA-ARRESTS”, append=FALSE)
# Add a second data set in a new worksheet
write.xlsx(Data2, file=”exportedata.xlsx”, sheetName=”MTCARS”,append=TRUE)
# Add a third data set
write.xlsx(Data3, file=”exportedata.xlsx”, sheetName=”TITANIC”,append=TRUE)
#Method two
# file : the path to the output file
# … : a list of data to write to the workbook
writetoexcel<- function (file, …)
{
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 = objnames[i])
else write.xlsx(objects[[i]], file, sheetName = objnames[i],
append = TRUE)
}
}
#The function writetoexcelworks for data frames, matrices, time series, and tables.
Test<-read.delim(“example.txt”,header=T,sep=’\t’,comment.char=””,quote=””,row.names=NULL, stringsAsFactors= FALSE )
nrow(test)
Data1<-test[1:10,] #first 10 rows of test file
Data2<-test[11:20,] #second 10 rows of test file
Data3<-test[21:30,] #third 10 rows of test file
#write Data1, Data2,Data3 into separated sheets in one excel.
xlsx.writeMultipleData(“exportedfile.xlsx”,Data1, Data2,Data3)