0

I've got the following code in R:

func.time <- function(n){
times <- c()
for(i in 1:n){

r <- 1 #x is the room the mouse is in
X <- 0 #time, starting at 0
while(r != 5){

if(r == 1){
    r <- sample(c(2,3),1) }

else if(r == 2){
    r <- sample(c(1,3), 1) }

else if(r == 3){
    r <- sample(c(1,2,4,5), 1) }

else if (r == 4){
    r <- sample(c(3,5), 1) }

 X <- X + 1
 }

 times <- c(X, times)
 }
 mean(times)
 }
 func.time(10000)

It works fine, but I've been told that using switch() can speed it up seeing as I've got so many if else statements but I can't seem to get it to work, any help is appreciated in advance.

Edit I've tried this:

func.time <- function(n) {
    times <- c() 
    for(i in 1:n) {
        r <- 1 #x is the room the mouse is in 
        X <- 0 #time, starting at 0 
        while(r != 5) { 
            switch(r, "1" = sample(c(2,3), 1),
                         "2" = sample(c(1,3), 1),
                         "3" = sample(c(1,2,4,5), 1),
                         "4" = sample(c(3,5))) 
            X <- X + 1 
        } 
        times <- c(X, times) 
    } 
    mean(times) 
} 

func.time(10000)

But it was a basic attempt, I'm not sure I've understood the switch() method properly.

Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
  • 1
    I recommend you read [this](http://stackoverflow.com/questions/7825501/switch-statement-usage) or [this](http://stackoverflow.com/questions/10393508/how-to-use-the-switch-statement-in-r-functions) or [this](http://www.inside-r.org/r-doc/base/switch) – ConfusedMan Mar 20 '15 at 22:03
  • 1
    And, rather than just "I can't seem to get it to work", it helps if you show what you tried. Then, rather than explaining the whole thing, we can explain the part that you don't get and correct an misconceptions. – Gregor Thomas Mar 20 '15 at 22:06
  • Hi, I've tried this: func.time <- function(n){ times <- c() for(i in 1:n){ r <- 1 #x is the room the mouse is in X <- 0 #time, starting at 0 while(r != 5){ switch(r, 1 = sample(c(2,3), 1) 2 = sample(c(1,3), 1) 3 = sample(c(1,2,4,5), 1) 4 = sample(c(3,5)) X <- X + 1 } times <- c(X, times) } mean(times) } func.time(10000) But it was a basic attempt, I'm not sure I've understood the switch() method properly. – user901823 Mar 20 '15 at 22:13
  • 1
    I've put your last comment in your post to ease "debugging"; First note that you need to quote numbers to have them accepted as arguments; you also need to separate each of them by a comma; then, the while loop is an infinite one since you increment X but r stays 1 all along. Finally, you'd want to assign the result of your switch statement to a variable, otherwise it doesn't serve any purpose as it is. – Dominic Comtois Mar 20 '15 at 23:01
  • So now we have drive-by reviewers rejecting useful edits. Arrrgh. – IRTFM Mar 20 '15 at 23:32

1 Answers1

1

I though Dominic's assessment was very useful but when I went to examine the edit it was being held up on what I thought was an incorrect basis. So I decided to just fix the code. When usign a numeric argument to the EXPR parameter you do not use the item=value formalism but rather just put in the expressions:

 func.time <- function(n){times <- c()
     for(i in 1:n){; r <- 1; X <- 0 
        while(r != 5){ 
            r <- switch(r, 
                  sample(c(2,3), 1) ,  # r=1
                  sample(c(1,3), 1) ,  # r=2
                  sample(c(1,2,4,5), 1), #r=3
                  sample(c(3,5), 1)  ) # r=4
            X <- X + 1 }
         times <- c(X, times) } 
         mean(times) } 

func.time(1000)
#[1] 7.999

For another example of how to use switch with a numeric argument to EXPR, consider my answer to this question: R switch statement with varying outputs throwing error

Community
  • 1
  • 1
IRTFM
  • 258,963
  • 21
  • 364
  • 487