Reputation: 5391
I'm trying to see how to store all my info in one table so i can print it at the end of the loop, here is the code I'm using
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)
}
I'd like to store each iteration in the IncrementalTable variable, at the moment i only know how to store one iteration at the time. How do i store it as a table to preserve all the iterations?
something like this
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
Upvotes: 0
Views: 15513
Reputation: 15163
The simplest way to get your answer is to set
IncrementalTable<-c()
at the top of your loop, and then replace the last line of your loop with this:
IncrementalTable <- rbind(IncrementalTable,c(i - 1,increment,f2,c2,f1,c1))
The result:
> 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
This will give you a plain matrix. You can then use
colnames(IncrementalTable)<-c("R","I","F2","C2","F1","C2")
to get the column names you want.
Upvotes: 2