简体   繁体   English

将多个数据框写入R中的excel文件的循环

[英]Loop for writing multiple dataframes to excel files in R

I have been splitting up dataframes and writing them to txt files in R, but now I've found out that they need to be written to xlsx files. 我一直在拆分数据帧,并将其写入R中的txt文件中,但是现在我发现它们需要写入xlsx文件中。 I installed the xlsx package and modified my loop, but it doesn't work anymore. 我安装了xlsx软件包并修改了我的循环,但是它不再起作用了。 I get "Error in [[.default (dots[[2L]], 1L) : subscript out of bounds". 我收到“ [[.default (dots [[2L]],1L):下标超出范围错误”。

Here is the loop: 这是循环:

trts<-vector("list", length=6)
trt<-as.character(c("CC", "CCW", "C2", "S2", "PF", "P"))

for(i in 1:6){
trts[[i]]<-co2[co2$trt == trt[i],]
  write.xlsx(trts[[i]], paste(trt[i], "CO2", "xlsx", sep="."))

Here is the data (my df is co2): Split this . 这是数据(我的df是co2): 拆分此

What's the deal? 这是怎么回事?

Why not just output it as CSV? 为什么不将其输出为CSV?

#Read Data
co2 <- read.table("~/Observed COBS CO2.txt",
                                header=T, quote="\"")

#Output to CSV
apply(as.matrix(unique(co2$trt)),1,
      function(x){
        write.table(co2[co2$trt == x,],
                    paste(x, "co2",".csv",sep=""),
                    sep=",",row.names=F)}
      )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM