-4

在单次迭代的不同阶段通过双循环生成数据时,如何将数据及其标签、标题、关于数据的信息(关于数据的信息)导出到单个 Excel 表中。代码:

 k=3
 t=2
   o=seq(from=1, to=t, by=1)
    for(i in 1:t){
        v=((o[i])+1)
        print ("treatments")
        print (v)
        h=seq(from=0, to=o[i], by=1)
        q1=NULL
        q2=NULL
        x1=NULL
           for(j in 1:o[i]){
              q1=c((v-(4*h[j]+1)))
              q2=c(((4*h[j])+2))
              T=c(q1,q2)
              y=c(0)
              IB=c(y,cumsum(T)%%v)
              print("The Initial Block(s) is/are=")
              print(IB)
                   p=seq(from=0, to=v-1, by=1)
                   l=NULL
                     for(i in 1:k){
                     for(j in 1:v){
                     l=c(l,rep((IB[i]+p[j]+v)%% v))
                     }
                    }
              x= matrix(c(l),nrow=k,ncol=v,byrow = TRUE)
              x1=cbind(x1,x)
                }
         print ("Design Matrix is")
         print (x1)
       }

在第一次迭代时,输出为:

        [1] "treatments"
        [1] 3
        [1] "The Initial Block(s) is/are="
        [1] 0 2 1
        [1] "Design Matrix is"
        [,1] [,2] [,3]
 [1,]    0    1    2
 [2,]    2    0    1
 [3,]    1    2    0

在第 2 次迭代中,输出为

  [1] "treatments"
  [1] 5
  [1] "The Initial Block(s) is/are="
  [1] 0 4 1
  [1] "The Initial Block(s) is/are="
  [1] 0 0 1
  [1] "Design Matrix is"
       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    1    2    3    4    0    1    2    3     4
 [2,]    4    0    1    2    3    0    1    2    3     4
 [3,]    1    2    3    4    0    1    2    3    4     0

等等 ...

请考虑

  1. 输出可能按照文件的单个 excel 表上提到的顺序排列。

  2. 矩阵可能是逐个单元格的——我已经尝试过,但它粘贴在单个单元格中。

  3. 请问有什么不明白的。

  4. 代码很长,所以请大家举个例子。

4

1 回答 1

2

郑重声明,这是一种可怕的、可怕的信息显示方式。

library(XLConnect)
wb <- loadWorkbook("~/Desktop/so/terrible_idea.xlsx",create = TRUE)
sht <- "Horrible Way To Display Data"
createSheet(wb,name = sht)

k=3
 t=2
   o=seq(from=1, to=t, by=1)
    for(i in 1:t){
        v=((o[i])+1)
        print ("treatments")
        appendWorksheet(wb,data.frame("treatments"),sheet = sht,header = FALSE)
        row_counter <- row_counter + 1
        print (v)
        appendWorksheet(wb,as.data.frame(v),sheet = sht,header = FALSE)
        h=seq(from=0, to=o[i], by=1)
        q1=NULL
        q2=NULL
        x1=NULL
           for(j in 1:o[i]){
              q1=c((v-(4*h[j]+1)))
              q2=c(((4*h[j])+2))
              T=c(q1,q2)
              y=c(0)
              IB=c(y,cumsum(T)%%v)
              print("The Initial Block(s) is/are=")
              appendWorksheet(wb,data.frame("The Initial Block(s) is/are="),sheet = sht,header = FALSE)
              print(IB)
              appendWorksheet(wb,as.data.frame(IB),sheet = sht,header = FALSE)
              row_counter <- row_counter + nrow(as.data.frame(IB))
                   p=seq(from=0, to=v-1, by=1)
                   l=NULL
                     for(i in 1:k){
                     for(j in 1:v){
                     l=c(l,rep((IB[i]+p[j]+v)%% v))
                     }
                    }
              x= matrix(c(l),nrow=k,ncol=v,byrow = TRUE)
              x1=cbind(x1,x)
                }
         print ("Design Matrix is")
         appendWorksheet(wb,data.frame("Design Matrix is"),sheet = sht,header = FALSE)
         print (x1)
         appendWorksheet(wb,as.data.frame(x1),sheet = sht,header = FALSE)
       }

saveWorkbook(wb)
于 2013-02-05T23:16:53.960 回答