title: How to change a selection of a POSIXct vector from YDM to YMD in R?
body: <p>I have a large dataset with POSIXct datetimes in a YMD_HMS format. I however came to realize that some dates were entered in the database in a YDM_HMS format instead of YMD_HMS. This is visible in the density-plot below.
  <a href="https://i.stack.imgur.com/WM5K8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WM5K8.png" alt="datetimes" /></a></p>
  <p>The observations run in march and april, but somehow the first of April through the 12th of April have been wrongly added, and are therefore spread across the year. Obviously I want to change tose YDM's into YMD's, but I fail...</p>
  <p>A reproducible example is this df:</p>
  <pre><code>df &lt;- data.frame(id = c(1:10), datetime = ymd_hms(&quot;2020-03-01 00:02:15&quot;,
                                                       &quot;2020-03-25 01:03:33&quot;,
                                                       &quot;2020-03-14 03:45:55&quot;,
                                                       &quot;2020-01-04 05:37:27&quot;,
                                                       &quot;2020-01-04 06:23:38&quot;,
                                                       &quot;2020-04-12 09:09:43&quot;,
                                                       &quot;2020-04-21 12:44:47&quot;,
                                                       &quot;2020-02-04 17:38:52&quot;,
                                                       &quot;2020-04-24 19:44:21&quot;,
                                                       &quot;2020-04-28 21:49:34&quot;,
                                                       tz = &quot;UTC&quot;))
  </code></pre>
  <p>I made an extra vector containing the dates, so I can target the faulty observations easily:</p>
  <p><code>df$date &lt;- as.Date(df$datetime)</code></p>
  <p>Then I thought I could easily use an <code>ifelse()</code>. The condition is the selection of dates which are wrong, the TRUE action is to cooerce it into character and then into ydm_hms, the FALSE action is to maintain datetime. This fails...</p>
  <pre><code>df &lt;- df %&gt;%
     ifelse(date %in% c(&quot;2020-01-04&quot;, &quot;2020-02-04&quot;), ydm_hms(as.character(datetime), tz = &quot;UTC&quot;), datetime)
  Error in ifelse(., date %in% c(&quot;2020-01-04&quot;, &quot;2020-02-04&quot;), ydm_hms(as.character(datetime),  : 
    unused argument (datetime)
  </code></pre>
  <p>Adding <code>mutate()</code> like this <code>df &lt;- df %&gt;% mutate(datetime = ifelse(date %in% c(&quot;2020-01-04&quot;, &quot;2020-02-04&quot;), ydm_hms(as.character(datetime), tz = &quot;UTC&quot;), datetime))</code> doesn't maintain a POSIXct  format for the datetime-vector:</p>
  <pre><code>&gt; df
     id   datetime       date
  1   1 1583020935 2020-03-01
  2   2 1585098213 2020-03-25
  3   3 1584157555 2020-03-14
  4   4 1578116247 2020-01-04
  5   5 1578119018 2020-01-04
  6   6 1586682583 2020-04-12
  7   7 1587473087 2020-04-21
  8   8 1580837932 2020-02-04
  9   9 1587757461 2020-04-24
  10 10 1588110574 2020-04-28
  </code></pre>
  <p>Question: how do I get this output:</p>
  <pre><code>   id            datetime
  1   1 2020-03-01 00:02:15
  2   2 2020-03-25 01:03:33
  3   3 2020-03-14 03:45:55
  4   4 2020-04-01 05:37:27
  5   5 2020-04-01 06:23:38
  6   6 2020-04-12 09:09:43
  7   7 2020-04-21 12:44:47
  8   8 2020-04-02 17:38:52
  9   9 2020-04-24 19:44:21
  10 10 2020-04-28 21:49:34
  </code></pre>