0

我正在尝试查看如何将我的所有信息存储在一张表中,以便我可以在循环结束时打印它,这是我正在使用的代码

 trips = 1000   
f1 = 0
f2 = 0
c1 = 18
c2= 12

 #Incremental with proportional factors
 factors = c(0,.35,0.25,0.20,.15,.05)
 n = 6
 for(i in 1:n)
 {
   if(c2 < c1)
   {
     increment = trips * factors[i]
     f2 = f2 + increment
     c2 = 12 + (.01*f2)     
    }
   else
  {
    increment = trips * factors[i]
    f1 = f1 + increment
    c1 = 18 + (.005*f1)
  }
  IncrementalTable = c(i - 1,increment,f2,c2,f1,c1)

}

我想将每次迭代存储在 IncrementalTable 变量中,目前我只知道如何存储一次迭代。我如何将其存储为表格以保留所有迭代?

像这样的东西

Incremental Table =    N  I    F2   C2    F1   C2
                       0  0    0    12    0    18
                       1 350   350  15.5  0    18.0
                       2 250   600  18    0    18
                       3 200   600  18    200  19
                       4 150   750  19.5  200  19
                       5 50    750  19.50 250  19.25
4

1 回答 1

1

获得答案的最简单方法是设置

IncrementalTable<-c()

在循环的顶部,然后将循环的最后一行替换为:

IncrementalTable <- rbind(IncrementalTable,c(i - 1,increment,f2,c2,f1,c1))

结果:

> IncrementalTable
     [,1] [,2] [,3] [,4] [,5]  [,6]
[1,]    0    0  750 19.5  250 19.25
[2,]    1  350  750 19.5  600 21.00
[3,]    2  250 1000 22.0  600 21.00
[4,]    3  200 1000 22.0  800 22.00
[5,]    4  150 1000 22.0  950 22.75
[6,]    5   50 1050 22.5  950 22.75

这会给你一个简单的矩阵。然后你可以使用

colnames(IncrementalTable)<-c("R","I","F2","C2","F1","C2")

获取您想要的列名。

于 2013-10-29T17:35:49.370 回答