title: R How to do a simple vector/matrix calculation with conditions applied?
body: <p>I have a vector i.e.
  change_vector (where dates are rownames)</p>
  
  <pre><code>2012-07-30  2012-08-06  2012-08-13  2012-08-20
  100%    100%    150%    100%
  </code></pre>
  
  <p>and a matrix:</p>
  
  <pre><code>    2012-07-30  2012-08-06  2012-08-13  2012-08-20
  2012-05-28  1.0 2.0 3.0 2.0
  2012-06-04  1.0 2.0 3.0 2.0
  2012-06-11  1.0 2.0 3.0 2.0
  2012-06-18  1.0 2.0 3.0 2.0
  2012-06-25  1.0 2.0 3.0 2.0
  2012-07-02  1.0 2.0 3.0 2.0
  2012-07-09  1.0 2.0 3.0 2.0
  2012-07-16  1.0 2.0 3.0 2.0
  2012-07-23  1.0 2.0 3.0 2.0
  2012-07-30  1.0 2.0 3.0 2.0
  2012-08-06  NA  2.0 3.0 2.0
  2012-08-13  NA  NA  3.0 2.0
  2012-08-20  NA  NA  NA  2.0
  2012-08-27  NA  NA  NA  NA
  </code></pre>
  
  <p>I want to do a simple calculation, if I multiply the vector with my matrix
  Then all the columns for 2012-08-13 will increase by 150%
  i.e. to 4.5.. see this example where I did the calculation </p>
  
  <pre><code>t(t(my_matrix) * (change_vector)) 
  
      2012-07-30  2012-08-06  2012-08-13  2012-08-20
  2012-05-28  1.0 2.0 4.5 2.0
  2012-06-04  1.0 2.0 4.5 2.0
  2012-06-11  1.0 2.0 4.5 2.0
  2012-06-18  1.0 2.0 4.5 2.0
  2012-06-25  1.0 2.0 4.5 2.0
  2012-07-02  1.0 2.0 4.5 2.0
  2012-07-09  1.0 2.0 4.5 2.0
  2012-07-16  1.0 2.0 4.5 2.0
  2012-07-23  1.0 2.0 4.5 2.0
  2012-07-30  1.0 2.0 4.5 2.0
  2012-08-06  NA  2.0 4.5 2.0
  2012-08-13  NA  NA  4.5 2.0
  2012-08-20  NA  NA  NA  2.0
  2012-08-27  NA  NA  NA  NA
  </code></pre>
  
  <p>However what I want is to limit the multiplication so that it only counts the last 9 weeks relative to that date, for instance, column 2012-08-13 only increases to 4.5 for dates 9 weeks relative to the uplift date
  i.e. the output should be:</p>
  
  <pre><code>    2012-07-30  2012-08-06  2012-08-13  2012-08-20
  2012-05-28  1.0 2.0 3.0 2.0
  2012-06-04  1.0 2.0 3.0 2.0
  2012-06-11  1.0 2.0 4.5 2.0
  2012-06-18  1.0 2.0 4.5 2.0
  2012-06-25  1.0 2.0 4.5 2.0
  2012-07-02  1.0 2.0 4.5 2.0
  2012-07-09  1.0 2.0 4.5 2.0
  2012-07-16  1.0 2.0 4.5 2.0
  2012-07-23  1.0 2.0 4.5 2.0
  2012-07-30  1.0 2.0 4.5 2.0
  2012-08-06  NA  2.0 4.5 2.0
  2012-08-13  NA  NA  4.5 2.0
  2012-08-20  NA  NA  NA  2.0
  2012-08-27  NA  NA  NA  NA
  </code></pre>
  
  <p>Please note in my real example, my matrix is quite large and the change vector will have a lot of different values</p>
  
  <p>Thanks</p>