title: How can I streamline this R script?
body: <p>I have a a big dataframe in R that all looks about like this:</p>
  
  <pre><code>    name   amount   date1       date2  days_out year
      JEAN  318.5 1971-02-16 1972-11-27  650 days 1971
   GREGORY 1518.5       &lt;NA&gt;       &lt;NA&gt;   NA days 1971
      JOHN  318.5       &lt;NA&gt;       &lt;NA&gt;   NA days 1971
    EDWARD  318.5       &lt;NA&gt;       &lt;NA&gt;   NA days 1971
    WALTER  518.5 1971-07-06 1975-03-14 1347 days 1971
     BARRY 1518.5 1971-11-09 1972-02-09   92 days 1971
     LARRY  518.5 1971-09-08 1972-02-09  154 days 1971
     HARRY  318.5 1971-09-16 1972-02-09  146 days 1971
     GARRY 1018.5 1971-10-26 1972-02-09  106 days 1971
  </code></pre>
  
  <p>If someone's days_out is less than 60, they get a 90% discount. 60-90, a 70% discount. I need to find out the discounted sum of all the amounts for each year. My utterly embarrassing workaround is to write a python script that writes an R script that reads like this for each relevant year: </p>
  
  <pre><code>tmp &lt;- members[members$year==1971, ]
  tmp90 &lt;- tmp[tmp$days_out &lt;= 60  &amp; tmp$days_out &gt; 0  &amp; !is.na(tmp$days_out),  ]
  tmp70 &lt;- tmp[tmp$days_out &lt;= 90  &amp; tmp$days_out &gt; 60 &amp; !is.na(tmp$days_out),  ]
  tmp50 &lt;- tmp[tmp$days_out &lt;= 120 &amp; tmp$days_out &gt; 90 &amp; !is.na(tmp$days_out),  ]
  tmp30 &lt;- tmp[tmp$days_out &lt;= 180 &amp; tmp$days_out &gt;120 &amp; !is.na(tmp$days_out),  ]
  tmp00 &lt;- tmp[tmp$days_out &gt; 180 | is.na(tmp$days_out), ]
  details.1971 &lt;- c(1971, nrow(tmp),
    nrow(tmp90), sum(tmp90$amount), sum(tmp90$amount) * .9,
      nrow(tmp70), sum(tmp70$amount), sum(tmp70$amount) * .7,
      nrow(tmp50), sum(tmp50$amount), sum(tmp50$amount) * .5,
      nrow(tmp30), sum(tmp30$amount), sum(tmp90$amount) * .9,
      nrow(tmp00), sum(tmp00$amount))
  membership.for.chart &lt;- rbind(membership.for.chart,details.1971)
  </code></pre>
  
  <p>It works just fine. The tmp frames and vectors get overwritten which is fine. But I know that I've utterly defeated everything that is elegant and efficient about R here. I launched R for the first time a month ago and I think I've come a long way. But I would really like to know how I should have gone about this?</p>