I have a large list with more than 100 data frames in it. For simplification I show a list with three data frames (dummy data). I would like to write each data frame into an excel file by using openxlsx. I already created a loop, in which each data frame becomes on excel file, but I want to name each excel file as the name of the data frame. How can I get each excel file named as the data frame in the list??
So DF1
should become DF1.xlsx. DF2
should become DF2.xlsx. DF
should become DF3.xlsx.But at the moment each excel file is name 1,2 or 3.
My loop:
for (i in (1:3)) {write.xlsx(listDF[i],file=paste(i,".xlsx"))}
# dummy data
listDF <- list(
DF1 = data.frame(
sample = c("TGX", "TGX", "TGX", "TGX"),
RC = c(0, 1, 2, 3),
medRC = c(0, 3, 4, 0),
RC.norm = c(0, 3, 3, 3),
medRC.norm = c(0, 3, 3, 3)
),
DF2 = data.frame(
sample = c("TBF", "TBF", "TBF", "TBF"),
RC = c(2, 1, 2, 3),
medRC = c(4, 3, 4, 0),
RC.norm = c(1, 3, 3, 3),
medRC.norm = c(0, 3, 3, 3)
),
DF3 = data.frame(
sample = c("TZW", "TZW", "TZW", "TZW"),
RC = c(4, 3, 2, 3),
medRC = c(1, 3, 2, 0),
RC.norm = c(1, 1, 1, 1),
medRC.norm = c(0, 7, 5, 3)
)
listDF
# $DF1
# sample RC medRC RC.norm medRC.norm
# 1 TGX 0 0 0 0
# 2 TGX 1 3 3 3
# 3 TGX 2 4 3 3
# 4 TGX 3 0 3 3
# $DF2
# sample RC medRC RC.norm medRC.norm
# 1 TBF 2 4 1 0
# 3 TBF 1 3 3 3
# 4 TBF 2 4 3 3
# 5 TBF 3 0 3 3
# $DF3
# sample RC medRC RC.norm medRC.norm
# 1 TZW 4 1 1 0
# 2 TZW 3 3 1 7
# 3 TZW 2 2 1 5
# 4 TZW 3 0 1 3