title: Discrepancy between R and Matlab speed
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7142767/why-are-loops-slow-in-r">Why are loops slow in R?</a>  </p>
  </blockquote>
  
  
  
  <p>Consider the following task. A dataset has 40 variables for 20,000 "users". Each user has between 1 and 150 observations. All users are stacked in a matrix called data. The first column is the id of the user and identifies the user. All id are stored in a 20,000 X 1 matrix called userid.</p>
  
  <p>Consider the following R code</p>
  
  <pre><code>useridl = length(userid)
  itime=proc.time()[3]    
  for (i in 1:useridl) {
  temp =data[data[,1]==userid[i],]
     }
   etime=proc.time()[3]
   etime-itime
  </code></pre>
  
  <p>This code just goes through the 20,000 users, creating the temp matrix every time. With the subset of observations belonging to userid[i]. It takes about 6 minutes in a MacPro.</p>
  
  <p>In MatLab, the same task </p>
  
  <pre><code>tic
  for i=1:useridl
  temp=data(data(:,1)==userid(i),:);
  end
  toc
  </code></pre>
  
  <p>takes 1 minute. </p>
  
  <p>Why is R so much slower? This is standard task, I am using matrices in both cases. Any ideas?</p>