I would like to use the apply family of functions in order to copy each file from one directory to the second sheet of each file in the second directory.
I have tried many things, ended up by making the following work, but only for a single file in the directory. How to apply it to all of the files in that folder?
setwd(".../r_path//390")
l1 <- list.files(pattern='*.xlsx')
r1 = lapply(l1, read.xlsx, sheetIndex=1, header=TRUE)
names(r1) <- l1
s1 = split(r1, names(r1))
setwd(".../r_path//390de")
l2 <- list.files(pattern='*.xlsx')
r2 = lapply(l2, read.xlsx, sheetIndex=1, header=TRUE)
names(r2) <- l2
s2 = split(r2, names(r2))
library(plyr)
library(xlsx)
l_ply (r1[1], function(x) write.xlsx(x,
file =paste0(names(s2[[1]])), sheetName = "TECO",
append = TRUE, row.names = FALSE))
The first excel file of a directory is copied as a second sheet to another excel file. But I want this to apply to all of the files in the folder. Any advice is welcome!
Here is my solution:
for (i in names(r1)) {
l_ply (r1[i], function(x) write.xlsx(x,file =paste0(names(s2[[i]])),
sheetName = "TECO", append = TRUE, row.names = FALSE))}