title: Script optimization, primarily due to for loops [R]
body: <p>I have a recording of blood pressure and velocity in the resolution of 1000 Hz. During this recording i have identified periods (e.g. 1, 2, 3). The primary problem is with optimization. I have multiple recordings which are between 10 and 15 minutes in length, which results in ~1.000.000 rows.</p>
  <pre><code>            n    time pres mcav period
  1           1   7.000   76 43.6      1
  2           2   7.001   75 43.6      1     
  3           3   7.002   73 43.6      1     
  4           4   7.003   74 43.6      1     
  5           5   7.004   74 43.5      1     
  6           6   7.005   74 43.5      1     
  ...
  898914 909995 916.994   84 60.8      3   
  898915 909996 916.995   85 60.7      3   
  898916 909997 916.996   85 60.7      3   
  898917 909998 916.997   84 60.6      3   
  898918 909999 916.998   83 60.4      3   
  898919 910000 916.999   84 60.3      3   
  </code></pre>
  <p>For each of these periods i would like to identify blocks (3 second periods).</p>
  <pre><code>temp &lt;- NULL
  
  #For loop for every period
  for(i in unique(df$period)){
  
   #Extract the part of the df which is within the period
   temp_df &lt;- df[df$time &gt;= min(df$time[df$period == i]) &amp; df$time &lt;= max(df$time[df$period == i]),]
  
   #Insert &quot;n&quot; starting from 1 and count from there.
   temp_df$block &lt;- temp_df$n-min(temp_df$n)+1
  
   #Divide this consecutive number into 3-second blocks. 
   temp_df$block &lt;- ceiling(temp_df$block/3000)
  
   #Combine the dataframes for every period into one.
   temp &lt;- rbind(temp,temp_df[,c(&quot;n&quot;,&quot;block&quot;)])
  }
  </code></pre>
  <p>This loop is actually pretty fast, but could be optimized. Is apply and a homemade function the way to go?</p>
  <p>Next part might be the issue. The temp-dataframe will now be merged:</p>
  <pre><code>df &lt;- merge(df,temp,by=&quot;n&quot;,all.x=T)
  </code></pre>
  <p>This part takes a couple of seconds, but it results in this dataframe:</p>
  <pre><code>            n    time pres mcav period block
  1           1   7.000   76 43.6      1     1
  2           2   7.001   75 43.6      1     1
  3           3   7.002   73 43.6      1     1
  4           4   7.003   74 43.6      1     1
  5           5   7.004   74 43.5      1     1
  6           6   7.005   74 43.5      1     1
  ...
  898914 909995 916.994   84 60.8      3   100
  898915 909996 916.995   85 60.7      3   100
  898916 909997 916.996   85 60.7      3   100
  898917 909998 916.997   84 60.6      3   100
  898918 909999 916.998   83 60.4      3   100
  898919 910000 916.999   84 60.3      3   100
  </code></pre>