title: Last Day of Month Function, given Year and Month
body: <p>I am unable to find what is specifically giving me this error after stepping through the code multiple times. I'm hoping that someone has seen this error before.</p>
  
  <p>Here's what I <em>think</em> is relevant to the problem:</p>
  
  <pre><code>#### Load packages
  library(lubridate)
  
  MONTH &lt;- 1
  YEAR &lt;- 2018
  
  # Last day of month
  last_day_of_mth &lt;- function(YEAR, MONTH){
    MONTH_BEGIN &lt;- as.POSIXlt(paste0(YEAR, "-", MONTH, "-1"))
    MONTH_END &lt;- MONTH_BEGIN
    day(MONTH_END) &lt;- day(MONTH_BEGIN)
    month(MONTH_END) &lt;- month(MONTH_END) + 1
    day(MONTH_END) &lt;- day(MONTH_END) - 1
    return(as.character(MONTH_END))
  }
  </code></pre>
  
  <p>This code loads a bunch of external files via <code>source()</code> which do not contain any other packages or other functions. They just query data from the Google Analytics API or they load .csv files, manipulate the data frames, and spit out appropriate output as .csv files.</p>
  
  <p>For some bizarre reason, after running these 10 files via <code>source()</code>, it spits out</p>
  
  <pre><code>&gt; last_day_of_mth(2018, 1)
  [1] "2018-03-02 06:00:00"
  </code></pre>
  
  <p>which is obviously not correct - it should be <code>2018-01-31</code>. </p>