0

We split the data frame column wise to export it to excel file. e.g

userid <- rep(1:2,times=4)
data1 <- replicate(8 , paste( sample(letters , 3 ) , collapse = "" ) )
data2 <- sample(10,8)
df <- data.frame( userid , data1 , data2 )
spt3 <- split( df , f = df$userid )

#  Split on userid
out <- split( df , f = df$userid )
#$`1`
#  userid data1 data2
#1      1   gjn     3
#3      1   yqp     1
#5      1   rjs     6
#7      1   jtw     5

#$`2`
#  userid data1 data2
#2      2   xfv     4
#4      2   bfe    10
#6      2   mrx     2
#8      2   fqd     9

now we want to export it to different excel (.xlsx) file

lapply(names(spt3), function(x) {write.table(spt3[[x]], file = paste("solid", x, sep = ""))})

This works for table output but not for excel file as output.I want different excel file for every split output. For instance , spt3[[1]], an excel file as output and similarly for spt3[[2]].

 Please note 
Thanks for suggestions. But I want (. Xlsx )file not  csv. E.g output should be like this in mentioned path  output1. Xlsx   output2. Xlsx  . Also diffeent excel files not different sheets of a single excel file .
  • replace with `write.csv()` and add the extension `.csv` inside `paste()` – joel.wilson Dec 14 '16 at 17:52
  • `lapply(names(spt3), function(x) {write.csv(spt3[[x]], file = paste0("path/", x,".csv"))})` – joel.wilson Dec 14 '16 at 17:53
  • Thanks for suggestions. But I want (. Xlsx )file not csv. E.g output should be like this in mentioned path – Kusum Sirole Dec 19 '16 at 04:32
  • Output1.Xlsx , output2.Xlsx .we need different excel files in outout not multiple sheets in a workbook. – Kusum Sirole Dec 19 '16 at 11:36
  • please go through `openxlsx` , try out examples, – joel.wilson Dec 19 '16 at 12:17
  • `lapply(names(spt3), function(x) {m<- createWorkbook();m1 <- createSheet(wb = m,sheetName = "USER");addDataFrame(x=spt3[[x]], sheet = m1);saveWorkbook(m, paste0("pathname/", x,".xlsx"))})` – joel.wilson Dec 19 '16 at 12:23
  • ThanX Joel , I have tried this . It's giving , error during wrap up : unused argument (wb=m , sheetName= "USER") – Kusum Sirole Dec 20 '16 at 12:40
  • it worked with me very well.. Im unsure of what you did wrong. You will have to explore now. bdw did you load `xlsx` package befor running the above command – joel.wilson Dec 20 '16 at 13:08
  • 1
    Yes it worked now . Actually I was using package open Xlsx , hence the error was coming . I would like to add a bit in a code to make it bit better . addDataFrame (x=spt [[x]],sheet=m1,row.names=F) which removes the additional column of row names . Code is perfect .thank you very much Joel wilson – Kusum Sirole Dec 21 '16 at 05:23
  • How to mark it as answer .. – Kusum Sirole Dec 21 '16 at 05:31
  • since this question was closed, now you can't..its okay!! happy i could finally help.. – joel.wilson Dec 21 '16 at 05:32

0 Answers0