0

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
6
  • There are nicer ways to do this than a for loop. Is the for loop of critical importance? Commented Oct 29, 2013 at 15:47
  • It is not entirely clear what you are doing. You don't specify c1 & c2 at the top, so the first pass through the loop will be problematic. Also, note that you are wiping out your previous version of IncrementalTable on each successive pass through the loop. Can you include what you want the output to look like, in addition? Commented Oct 29, 2013 at 15:50
  • No, but i know the for loop so I'm using that. Let me edit the script text to include all the variables. Commented Oct 29, 2013 at 15:53
  • I understand your problem better and a loop seems the easiest way. Check out rbind to figure out how to record each row. However, try to pre-allocate your final data.frame and update rows in each iteration, rather than inserting new rows. Commented Oct 29, 2013 at 16:17
  • Im still not sure how to approach it. is there an example that you could provide? Commented Oct 29, 2013 at 16:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I did something similar by decleraing a variable as null before the loop and then in the loop using rbind() and data.frame and colnames() to change my headings. SAT = NULL Inside loop: SAT = rbind(SAT, data.frame(i,phi,f2,c2,f1,c1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.