title: How can I calculate the mean of a series of data, which has different exposure period
body: <p>Can anyone help? I am new to R and data analysis.</p>
  
  <p>I am trying to calculate the mean exposure level of the air pollution concentration in different districts. Here I have two data sets: (1) Pollutant; (2)Exposure period. Their formats look like below:</p>
  
  <pre><code>(1) Pollutant
  pollutant  district  Jun-1996  Jul-1996 ........... Apr-1997 May-1997
    SO2         A         50        45                   56       60
    SO2         B         41        43                   50       42
    SO2         C         ..........................................
    NO2         A          ..........................................
    NO2         B           ..........................................
    NO2         C          ..........................................
    ... 
  
  
  (2)Exposure period
  ID   district     start      end
  1       A        Jun-1996   Apr-1997  
  2       A        Aug-1996   Apr-1997
  3       C        Jul-1996   May-1997
  4       B        Sep-1996   Apr-1997
  5       C        Aug-1996   May-1997
  ...
  ...
  ...
  </code></pre>
  
  <p>What I want to calculate is the mean exposure of each kind of pollutant, for each ID, within the period of the ID got exposure, and in the particular district the ID lived in.</p>
  
  <p>Taking the first ID as an example:</p>
  
  <pre><code>He is exposed to SO2, NO2  
  He got exposed during Jun-1996 to Apr-1997
  He lived in District A
  </code></pre>
  
  <p>Thus the ideal results would be like:</p>
  
  <pre><code>ID  SO2_ave  NO2_ave
  1      xx      xx
  2
  3
  </code></pre>
  
  <p>So that after calculated, I could add the mean value as new column into the Exposure data set.</p>
  
  <p>Thank you very much!!!</p>