title: Group dates by week in R
body: <p>I wish to label my data based on their week. This is my data: </p>
  
  <pre><code>df2 &lt;- structure(list(Order_Date = structure(c(16735, 16805, 16753, 
  16830, 17075, 17009, 17085, 16740, 16891, 16750, 16820, 16849, 
  16906, 16929, 16746, 16731, 16786, 16873, 16895, 16931), class = "Date")), .Names = "Order_Date", row.names = c(NA, 
  -20L), class = "data.frame")
  </code></pre>
  
  <p>and I tried to label them based on the week (0th week, 1th week and ....) and I wish to group my data by the week later</p>
  
  <p>And I tried this:</p>
  
  <pre><code># order by data
  library (dplyr)
  df2&lt;- arrange(df2, Order_Date)
  
  
  # label them by week
  $df2$week &lt;- cumsum(weekdays(df2$Order_Date) == "Friday")
  </code></pre>
  
  <p>It doesn't give me the correct result and I have the following output, which is weird</p>
  
  <pre><code>   Order_Date week
  1  2015-10-27    0
  2  2016-01-05    0
  3  2015-11-14    0
  4  2016-01-30    0
  5  2016-10-01    0
  6  2016-07-27    0
  7  2016-10-11    0
  8  2015-11-01    0
  9  2016-03-31    0
  10 2015-11-11    0
  11 2016-01-20    0
  12 2016-02-18    0
  13 2016-04-15    1
  14 2016-05-08    1
  15 2015-11-07    1
  16 2015-10-23    2
  17 2015-12-17    2
  18 2016-03-13    2
  19 2016-04-04    2
  20 2016-05-10    2
  </code></pre>
  
  <p>Ideally, I'd like to have this output:</p>
  
  <pre><code>  Order_Date   label
  1  2015-10-23   0
  2  2015-10-27   0
  3  2015-11-01   1
  4  2015-11-07   2
  5  2015-11-11   2
  6  2015-11-14   3
  7  2015-12-17   8
  8  2016-01-05   10
  </code></pre>
  
  <p>since row number 8 occurs 10 week after row number 1 but also a solution that generates the following is my second alternative that shows these data are not in the same week:</p>
  
  <pre><code>  Order_Date   label
  1  2015-10-23   0
  2  2015-10-27   0
  3  2015-11-01   1
  4  2015-11-07   2
  5  2015-11-11   2
  6  2015-11-14   3
  7  2015-12-17   4
  8  2016-01-05   5
  </code></pre>