title: Loop that matches row to column names and computes an average of the 3 preceding columns
body: <p>Im trying to make some computations in R. I have a dataset where in the columns i have id, startdate and then every day date from 2014 till 2017.
  Now every id has a different start date. Accompanied for every date are concentrations of a chemical specific for an individual id.</p>
  
  <p>A sample from my data looks like this:</p>
  
  <pre><code>  id     time 20140101 20140102 20140103 20140104 20140105 20140106 20140107
  1  1 20141119      2.6      2.5      4.1      4.8      3.1      1.8      3.5
  2  4 20150403      1.7      1.6      2.8      3.4      2.0      1.2      1.9
  3  7 20140104      2.2      2.2      3.7      4.4      2.6      1.3      2.9
  4  8 20141027      2.7      2.5      4.1      4.9      3.3      1.8      3.6
  5  9 20141112      2.6      2.4      3.9      4.7      3.1      1.7      3.4
  </code></pre>
  
  <p>Now what i would like to do is to run a script that loops trough each row id and time combo eg "1 20141119" or "8 20141027", and matches the date numbers to the colnames and give me the corresponding concentration values.
  so the combo "7 20140104" gives me the concentration 4.4
  After this i would like to do the same but then take the date and make a 3 day average preceding the time date. So for the combo "7 20140104" make an average of the dates 20140102 20140103 20140104 concentrations for id 7</p>
  
  <p>I made a small test data frame</p>
  
  <pre><code>id &lt;- 12:18
  date &lt;- c("c","d","e","f","c","d","e")
  a &lt;- rnorm(7, 2, 1)
  b &lt;- rnorm(7, 2, 1)
  c &lt;- rnorm(7, 2, 1)
  d &lt;- rnorm(7, 2, 1)
  e &lt;- rnorm(7, 2, 1)
  f &lt;- rnorm(7, 2, 1)
  df &lt;- data.frame(id, date, a, b, c, d, e, f)
  </code></pre>
  
  <p>This was my solution for the first part of my question.</p>
  
  <pre><code>for(i in 1:nrow(df)){
    conc &lt;- df[i, df[i,"date"]==colnames(df)]
    print(conc)
    }
  </code></pre>
  
  <p>which works enough for the first part, but currently i don't know how to do the 3 day average. If you have tips on how to do the first part more nicely im all ears.
  Hopefully you people can help me.
  Thanks very much for your help.</p>