title: dual index loops in R
body: <p>For coding aficionados this may be a no-brainer but as someone new to these tools it's been twisting my noodle for a while now.</p>
  
  <p>Essentially I have some chemometric data that I wish to adjust against a series of blanks run at intervals through the sample analysis.  All the outputs are time stamped and I want to use the proportional ratio of the interval between the sample and the next blank over the interval between two successive blanks, plus the last blank value, as a modifier for a simple interpolation of the correction factor applied.  When the sample time value is later than the second blank I want to increment the blank pair so that I'm applying a correction based on the new pair of blanks for samples that fall in between their time periods.
  My data set contains a range of elements and the blank corrections differ between elements. </p>
  
  <p>Data set:</p>
  
  <pre><code>Blanks
     Sample           Date/Time Location Li(ppb) Mg(ppb) Ca(ppm) Cr(ppb) Mn(ppb) Co(ppb) Ni(ppb) Cu(ppb) Zn(ppb) Rb(ppb) Sr(ppb) Cd(ppb) Ba(ppb) Ce(ppb) Gd(ppb) Pb(ppb) U(ppb)
  1  Blank0 2016-05-23 19:54:33        B   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000   0.000       0   0.000   0.000       0   0.000  0.000
  7  Blank1 2016-05-23 20:35:25        B  -0.069   0.081   0.005  -0.007  -0.001   0.000  -0.007   0.005  -0.004   0.000   0.005       0  -0.003   0.000       0   0.000  0.005
  18 Blank2 2016-05-23 21:50:30        B  -0.076   0.743   0.020  -0.005   0.002  -0.001  -0.008   0.002   0.220   0.001   0.042       0   0.008   0.002       0   0.002  0.000
  29 Blank3 2016-05-23 23:05:43        B  -0.093  -0.080   0.000  -0.020  -0.001  -0.001  -0.010   0.001  -0.009   0.000  -0.001       0   0.001   0.000       0   0.000  0.000
  38 Blank4 2016-05-24 00:07:19        B  -0.096  -0.045   0.000  -0.015   0.000  -0.001  -0.001  -0.002   0.002   0.000  -0.001       0   0.000   0.000       0   0.000  0.000
  </code></pre>
  
  <p>Data: Mdat_samp</p>
  
  <pre><code>     Sample           Date/Time Location Li(ppb) Mg(ppb) Ca(ppm) Cr(ppb) Mn(ppb) Co(ppb) Ni(ppb) Cu(ppb) Zn(ppb) Rb(ppb) Sr(ppb) Cd(ppb) Ba(ppb) Ce(ppb) Gd(ppb) Pb(ppb) U(ppb)
  2  PK-Std1p 2016-05-23 20:01:22        S  -0.013  111.10   31.28   1.050   0.121   0.910   0.157   0.134   0.141   0.010   35.28   0.010   0.145   0.113   0.123   0.036  0.897
  3  PK-Std2p 2016-05-23 20:08:10        S   0.018  216.60   41.17   5.095   0.491   4.654   0.294   0.533   0.511   0.048   71.03   0.047   0.500   0.456   0.240   0.081  4.497
  4  PK-Std3p 2016-05-23 20:14:59        S   0.057  303.90   48.90   9.695   1.215   9.332   0.487   1.294   1.180   0.093  105.30   0.093   1.164   1.128   0.467   0.132  9.057
  5  PK-Std4p 2016-05-23 20:21:48        S   0.234  391.80   57.01  14.060   4.903  14.100   0.704   5.100   4.597   0.239  141.40   0.231   4.583   4.556   0.703   0.965 13.730
  6  PK-Std5p 2016-05-23 20:28:36        S   0.477  474.20   64.35  29.560   9.621  30.050   0.908   9.985   9.030   0.467  173.20   0.459   8.909   8.956   0.913   4.506 29.350
  8       L1W 2016-05-23 20:42:14        L   0.037  141.90   33.48   0.003   0.714   0.020   0.113   0.200   0.217   0.007   72.64   0.001   0.125   0.008   0.001   0.037  0.012
  9       L2W 2016-05-23 20:49:03        L   0.023  131.50   38.09  -0.003   0.590   0.014   0.106   0.134   0.694   0.004   75.96   0.001   0.115   0.008   0.001   0.031  0.008
  10      L3W 2016-05-23 20:55:54        L   0.078  208.20   52.54  -0.002   0.843   0.021   0.153   0.180   0.119   0.005  110.40   0.002   0.172   0.011   0.001   0.054  0.009
  11     L17W 2016-05-23 21:02:43        L   0.025  152.40   38.64  -0.008   0.658   0.016   0.117   0.270   0.121   0.006   82.45   0.001   0.108   0.008   0.001   0.042  0.007
  12     L18W 2016-05-23 21:09:32        L   0.070  204.30   54.29  -0.005   1.311   0.023   0.172   0.417   0.197   0.008  117.00   0.002   0.144   0.010   0.001   0.057  0.010
  13     L19W 2016-05-23 21:16:21        L   0.045  181.40   51.45  -0.004   0.701   0.020   0.162   0.296   0.160   0.008  109.20   0.001   0.116   0.009   0.001   0.055  0.009
  14     L20W 2016-05-23 21:23:09        L   0.022  150.30   38.55   0.000   0.596   0.016   0.118   0.223   0.148   0.007   77.44   0.001   0.114   0.007   0.001   0.048  0.006
  15     L21W 2016-05-23 21:29:59        L   0.010  132.70   34.72  -0.006   0.679   0.014   0.110   0.249   0.153   0.007   67.33   0.001   0.098   0.008   0.001   0.034  0.007
  16     L23W 2016-05-23 21:36:49        L   0.035  210.00   48.16   0.006   1.230   0.020   0.155   0.370   0.202   0.008  110.20   0.001   0.251   0.012   0.001   0.055  0.010
  17     L24W 2016-05-23 21:43:39        L   0.051  193.00   56.83   0.024   0.825   0.024   0.189   0.404   0.205   0.007  113.80   0.002   0.135   0.010   0.001   0.044  0.010
  19      N1W 2016-05-23 21:57:20        N   0.041  205.10   49.06   0.005   1.077   0.021   0.157   0.481   0.143   0.006  102.90   0.002   0.145   0.012   0.001   0.049  0.012
  20      N4W 2016-05-23 22:04:10        N  -0.002  140.90   38.72   0.008   0.887   0.016   0.131   0.312   0.130   0.005   76.39   0.001   0.133   0.010   0.001   0.032  0.011
  21      N7W 2016-05-23 22:11:00        N   0.005  134.40   40.92   0.002   0.651   0.019   0.136   0.204   0.122   0.005   90.48   0.002   0.158   0.010   0.001   0.034  0.012
  22     N10W 2016-05-23 22:17:50        N   0.010  150.10   40.86  -0.001   0.714   0.018   0.132   0.199   0.089   0.004   81.78   0.001   0.149   0.010   0.001   0.029  0.012
  23     N11W 2016-05-23 22:24:40        N   0.028  156.10   43.10  -0.007   0.870   0.019   0.139   0.292   0.099   0.005   89.83   0.001   0.134   0.010   0.001   0.034  0.009
  24     N12W 2016-05-23 22:31:31        N   0.015  158.60   43.03   0.019   0.807   0.018   0.145   0.253   0.113   0.006   87.60   0.001   0.117   0.011   0.001   0.039  0.009
  25     N13W 2016-05-23 22:38:21        N   0.021  157.50   44.94   0.038   0.833   0.020   0.159   0.245   0.124   0.007   92.04   0.001   0.118   0.011   0.001   0.034  0.011
  26     N14W 2016-05-23 22:45:12        N   0.041  171.80   49.04   0.008   1.115   0.020   0.165   0.226   0.123   0.006  109.00   0.002   0.177   0.011   0.001   0.044  0.010
  27     N18W 2016-05-23 22:52:02        N   0.050  164.60   49.49   0.019   1.555   0.022   0.162   0.735   2.740   0.006  104.50   0.005   4.152   0.011   0.001   0.180  0.008
  28     N23W 2016-05-23 22:58:52        N   0.001  132.30   36.21  -0.008   1.578   0.015   0.110   0.838   3.751   0.004   73.86   0.008   6.080   0.009   0.001   0.257  0.009
  30     P10W 2016-05-23 23:12:33        P  -0.003  139.50   36.40  -0.005   0.537   0.018   0.123   0.334   0.172   0.005   76.07   0.002   0.127   0.008   0.001   0.033  0.008
  31     P16W 2016-05-23 23:19:24        P   0.057  172.90   55.53  -0.003   0.689   0.025   0.186   0.253   0.482   0.004  124.10   0.001   0.122   0.010   0.001   0.041  0.010
  32     P17W 2016-05-23 23:26:15        P   0.018  193.80   44.01   0.000   0.780   0.020   0.141   0.269   0.139   0.004   99.34   0.001   0.124   0.011   0.001   0.031  0.010
  33     P19W 2016-05-23 23:33:06        P   0.040  181.50   51.98   0.027   0.762   0.026   0.173   0.320   0.191   0.008  108.70   0.002   0.162   0.011   0.001   0.043  0.009
  34     P20W 2016-05-23 23:39:57        P   0.049  182.50   53.05   0.007   0.676   0.024   0.180   0.298   0.147   0.007  106.80   0.001   0.133   0.012   0.001   0.033  0.011
  35     P21W 2016-05-23 23:46:48        P  -0.005  147.60   39.68   0.010   0.701   0.017   0.135   0.240   0.165   0.007   81.14   0.001   0.111   0.009   0.001   0.034  0.008
  36     P22W 2016-05-23 23:53:39        P   0.008  181.40   41.83   0.031   0.789   0.026   0.157   0.604   0.295   0.010   92.27   0.002   0.172   0.015   0.002   0.032  0.017
  37     P25W 2016-05-24 00:00:30        P  -0.016  124.30   31.92   0.028   0.596   0.015   0.104   0.265   0.187   0.004   64.91   0.002   0.080   0.008   0.001   0.049  0.007
  39  PK-Std1 2016-05-24 00:14:07        S  -0.089   98.38   28.27   0.957   0.125   0.990   0.194   0.156   0.152   0.010   35.49   0.010   0.145   0.116   0.121   0.035  0.890
  40  PK-Std2 2016-05-24 00:20:56        S  -0.041  198.80   38.35   4.812   0.504   4.968   0.338   0.570   0.527   0.049   71.38   0.051   0.503   0.465   0.237   0.080  4.520
  41  PK-Std3 2016-05-24 00:27:45        S   0.012  289.00   46.70   9.434   1.245   9.890   0.555   1.383   1.227   0.094  105.40   0.094   1.191   1.138   0.472   0.134  9.091
  42  PK-Std4 2016-05-24 00:34:34        S   0.166  383.40   56.11  13.920   5.029  14.930   0.771   5.473   4.775   0.241  143.00   0.239   4.623   4.637   0.707   0.962 14.120
  43  PK-Std5 2016-05-24 00:41:22        S   0.427  462.20   63.08  29.060   9.831  31.610   0.972  10.650   9.258   0.475  174.30   0.461   8.977   9.092   0.916   4.532 29.790
  </code></pre>
  
  <p>My latest coding attempt is:</p>
  
  <pre><code>    j=1     
  for (i in 1:nrow(Mdat_samp))
          {       
          divisor&lt;-(as.double(difftime(Blanks[j+1, 2], Blanks[j,2]))-as.double(difftime(Blanks[j+1,2], Mdat_samp[i,2])))/as.double(difftime(Blanks[j+1, 2], Blanks[j,2])) 
          adjust&lt;- (Blanks[j, 4:20]+(Blanks[j+1, 4:20]*divisor))
          write.table((Mdat_samp[i,4:20]-adjust), file="Baseadjust.txt", append=TRUE, col.names=FALSE, row.names=FALSE, sep=",")
          if(as.double(difftime(Mdat_samp[j,2], Blanks[j,2]))&lt;0) j=j+1
          if(j+1&gt;nrow(Blanks)) break
          }
  </code></pre>
  
  <p>What doesn't seem to happen is for j to index properly so I end up applying the same correction to all subsequent lines rather than calculating and applying the correction for the specific line.  As I see it I have two indexes running, the 'i' in the "for loop" and the 'j' in the "if loop", but only one is iterating correctly.<br>
  Sorry for the long post, and probably very poor formatting, I wanted to put as much information in as possible, but couldn't find the tools to post the data sets plainly.  I hope they're are not too odious to look at.  </p>