title: R - left_join dataframe with loop
body: <p>my dataframe x looks like this</p>
  
  <pre><code>    id 
  1   42
  </code></pre>
  
  <p>My second data.frame z is:</p>
  
  <pre><code>   id amoumt     date
  1  42      3  2013-09
  2  42      8  2013-09
  3  42      1  2011-09
  </code></pre>
  
  <p>The goals are:</p>
  
  <ul>
  <li>find lowest and highest date</li>
  <li>calculate amount for each month (if there are more amounts from one id and same, calculate it into one row)</li>
  <li>create column with date and append it into the x dataframe (keep in mind that in my project range will be more that 10 years).</li>
  </ul>
  
  <p>I have created this code:</p>
  
  <pre><code>dates &lt;- as.character(seq(as.Date(min(z$date), max(z$date), by=c("months"))))
  </code></pre>
  
  <p>It finds first and last date. And then I have this loop, which do not works:</p>
  
  <pre><code>for (d in dates) {
    z %&gt;%
      group_by(id) %&gt;%
      summarise(amount = sum(amount)) %&gt;%
      setNames(c(names(.)[1], paste("in", format(as.Date(d), "%Y-%m"), sep=" "))) %&gt;%
      left_join(
        .,
        x,
        by=c("id")        )
  }
  </code></pre>
  
  <p>Result should look like this:</p>
  
  <pre><code>   id 2011-09    2013-09
  1  42       0         11
  2  42       1          0
  </code></pre>