-1

I'm trying to set up a set of for loops to nest outside a series of if loops. I have a set of parameters I'm trying to cycle through but struggling to set up my code to run through each. I'm really new to R and I'm aware for loops aren't ideal but not sure that the apply family could be used here

So when I run

for(i in seq(from=0.05, to=0.5, by=0.05)){

for(c in 1:5) {
test[c,2] <- i
test[c,1] <- c
c = c + 1
}
print (i)
}
test
    C   I
  1 1 0.5
  2 2 0.5
  3 3 0.5
  4 4 0.5
  5 5 0.5

Ideally I want to get it so I produce a table with the run number, value of the parameter and frequency of another value side by side but struggling to do so.

Effectively I want to find a way to make it:

    C   I
  1 1 0.05
  2 2 0.05
  3 3 0.05
  4 4 0.05
  5 5 0.05
  1 1 0.1
  2 2 0.1
  3 3 0.1
  4 4 0.1
  5 5 0.1
Gibbon
  • 3
  • 4
  • 2
    Is not clear how is the output you want! – Robert Jun 21 '16 at 20:31
  • I'm trying to get it to produce a data frame which will have the columns: Run number, the value of i and frequency as the aim of this code is to have it contain a series of if loops which are modelling an epidemiological model so it would be the i/parameter loop with the simulation number nested inside that with the if loops then nested within them – Gibbon Jun 21 '16 at 20:32
  • Maybe you want `expand.grid(i = seq(from=0.05, to=0.5, by=0.05), c = 1:5)`? – Gregor Thomas Jun 21 '16 at 20:43
  • I'm struggling to get the loops to run at all, it just seems to give me the final value of i for each value of c rather than running through them all so I'm not sure if this suggestion solves my issue. I'm really inexperienced with R so I'm probably missing the point somewhere – Gibbon Jun 21 '16 at 20:55
  • The biggest thing you could do is [make your example reproducible](http://stackoverflow.com/q/5963269/903061). It's still not very clear what your desired output is. – Gregor Thomas Jun 21 '16 at 20:56
  • Your inner loop (with `c`) doesn't really do anything. For each value of `i`, the inner loop sets `c` to 1, then the code inside adds 1 so `c` is 2. That iteration is done so the loop then sets `c` to 2, then code inside adds 1 so `c` is 3. That iteration is done so the loop sets `c` to 3... eventually `c` is 6, but nothing has been accomplished by cycling through these `c` values. Then `i` is printed. Then the next iteration begins for the next `i`. – Gregor Thomas Jun 21 '16 at 20:59
  • Because the inner loop doesn't interact with anything other than `c`, and `c` isn't used outside of the inner loop, the inner loop accomplishes nothing and could be deleted without changing anything. Your outer loop sets the second column of `test` to `0.05`, then prints `.05`. Then it overwrites its previous change, setting the second column of test to `.1`, then prints `.1`, ... the only net change at the end is that the second column of test will have the value of the last value of `i`. – Gregor Thomas Jun 21 '16 at 21:02
  • That makes sense. I updated my post but effectively I want to run through 1:5 (so c) for each value of i from 0.05 through 0.5 but not sure how to achieve this – Gibbon Jun 21 '16 at 21:06
  • I tried to make it more reproducible but not sure if I've managed to make it more understandable – Gibbon Jun 21 '16 at 21:40
  • The for loop answer is that the meat of what happens (which should be modifying values for a single row of a data frame) needs to happen inside the *innermost* loop. The R answer is my first comment: you don't need a `for` loop at all, just `expand.grid(i = seq(from=0.05, to=0.5, by=0.05), c = 1:5)`. – Gregor Thomas Jun 21 '16 at 23:00
  • I am just not then sure how to get my other loops I have to run through each value of i and repeat it for 1 to c – Gibbon Jun 22 '16 at 08:30

1 Answers1

0

not sure if this solves your question:

is <- seq(from=0.05, to=0.5, by=0.05)
test<-matrix(NA,nrow=length(is),ncol=3)
test[,1]=1:length(is)
for(i in test[,1]){
  test[i,2] <- is[i]

  for(c in 1:5) {
    c = c + 1
  }
  test[i,3]<-c
  print (is[i])
}    
test

> test
      [,1] [,2] [,3]
 [1,]    1 0.05    6
 [2,]    2 0.10    6
 [3,]    3 0.15    6
 [4,]    4 0.20    6
 [5,]    5 0.25    6
 [6,]    6 0.30    6
 [7,]    7 0.35    6
 [8,]    8 0.40    6
 [9,]    9 0.45    6
[10,]   10 0.50    6
Robert
  • 5,038
  • 1
  • 25
  • 43
  • This is very nearly what I am after, is there a way to use it to repeat it x number of times for each value of i? – Gibbon Jun 21 '16 at 20:43