title: Faster if statement evaluations in R
body: <p>So I am trying to figure out if there is a better way to add multiple conditional statements to an if clause in <code>R</code> in order to speed up the process. Below is some code that I wrote that runs very fast on large datasets in the simple case and not so fast on the not so simple case.  Any suggestions are greatly appreciated!  Also, the tic-toc function is at the very bottom on the question in case you would like to run it yourself and see how fast the function runs.</p>
  
  <p>Also, to give some intuition of what the code is doing, the first chunk is simply determining if there are any pairs of x's and y's that have larger values than all of the other x's and y's.</p>
  
  <p>The second chunk of code is doing the same thing, however, it adds the condition that is any of the x values are actually equal to each other then check to see which one has the lowest y value.  Likewise, if any of the y values are equal to each other than check to see which one has the lowest x value.</p>
  
  <p>So, running the code in the simple case I have the following:</p>
  
  <pre><code>tic()
  
  x = runif(10000)
  y = runif(10000)
  
  front = 1:length(x)
  
  for(i in 1:length(x)){
      for(n in 1:length(x)){
          if((x[i]&gt;x[n]  &amp;  y[i]&gt;y[n])){
              front[i] = NA
              break
          }
      }
  }
  
  toc()
  </code></pre>
  
  <p>So as you can see, I am only evaluating the single condition that <code>x[i]&gt;x[n]  &amp;  y[i]&gt;y[n]</code></p>
  
  <blockquote>
    <p>toc()
        elapsed 
           1.28</p>
  </blockquote>
  
  <p>and the code above runs in 1.28 seconds.
  Now, running the code when I have three conditions to check I have the following: </p>
  
  <pre><code>tic()
  
  x = runif(10000)
  y = runif(10000)
  
  front = 1:length(x)
  
  for(i in 1:length(x)){
      for(n in 1:length(x)){
          if((x[i]&gt;x[n]  &amp;  y[i]&gt;y[n]) | (x[i]==x[n] &amp; y[i]!=min(y[which(x==x[i])])) | (y[i]==y[n] &amp; x[i]!=min(x[which(y==y[i])]))){
              front[i] = NA
              break
          }
      }
  }
  
  
  toc()
  </code></pre>
  
  <p>so as you can see, I now have to check three conditions inside my if statement, namely, </p>
  
  <pre><code>(x[i]&gt;x[n]  &amp;  y[i]&gt;y[n]) | (x[i]==x[n] &amp; y[i]!=min(y[which(x==x[i])])) | (y[i]==y[n] &amp; x[i]!=min(x[which(y==y[i])]))
  </code></pre>
  
  <p>however, this leads to a huge computational burden in <code>R</code> and make the code much more slow.</p>
  
  <pre><code>&gt; toc()
  elapsed 
    74.47
  </code></pre>
  
  <p>We see that running the newly adapted code its now slowed down considerably to 74.47 seconds. Now I am looking for either alternative function calls that would speed up my code or simply rewriting it in a "better" way that the code is not so slow. </p>
  
  <p>Here is the code for the tic-toc function if needed:</p>
  
  <pre><code>tic &lt;- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
  {
     type &lt;- match.arg(type)
     assign(".type", type, envir=baseenv())
     if(gcFirst) gc(FALSE)
     tic &lt;- proc.time()[type]         
     assign(".tic", tic, envir=baseenv())
     invisible(tic)
  }
  
  toc &lt;- function()
  {
     type &lt;- get(".type", envir=baseenv())
     toc &lt;- proc.time()[type]
     tic &lt;- get(".tic", envir=baseenv())
     print(toc - tic)
     invisible(toc)
  }
  </code></pre>
  
  <p><strong>EDIT for sashkello</strong></p>
  
  <p>So my code now looks like this:</p>
  
  <pre><code>library(mvtnorm)
  #Here are the variables I will be working with 
  
  &gt; x
   [1] 0.53137100 0.75357474 0.87904120 0.29727488 0.00000000 0.00000000
   [7] 0.00000000 0.00000000 0.00000000 0.04059217
  &gt; y
   [1]  4.873500  3.896917  1.258215  5.776484 12.475491  5.273784 13.803158
   [8]  4.472204  2.629839  6.689242
  &gt; front
   [1] NA NA  3 NA NA NA NA NA  9 NA
  &gt; all.preds
  [1] 0.596905183 0.027696850 1.005666896 0.007688514 3.900000000
  
      x = x[!is.na(front)]
      y = y[!is.na(front)]
  
      mu = c(all.preds[1],all.preds[3])
      sigma = matrix(c(all.preds[2],0,0,all.preds[4]),nrow=2)
  
      z = rmvnorm(10000,mu,sigma)
      z[,1] = sapply(z[,1],function(x){max(x,0)})
  
      points(z,col="black",pch=19,cex=.01)
      temp = 1:nrow(z)
  
      for(i in 1:length(temp)){
          cond1 = z[i,2]!=min(z[which(z[,1]==z[i,1]),2])
          cond2 = z[i,1]!=min(z[which(z[,2]==z[i,2]),1])
          for(n in 1:length(x)){
              if((z[i,1]&gt;x[n]  &amp;  z[i,2]&gt;y[n]) | (z[i,1]==x[n] &amp; cond1) | (z[i,2]==y[n] &amp; cond2)){
                  temp[i] = NA
                  break
              }
          }
      }
      prop = sum(!is.na(temp))/length(temp)
  </code></pre>
  
  <p>and that cond1 and cond2 statement still take horribly long.  Any suggestions?</p>