title: Convert factor to date object R without NA
body: <p><strong>Question:</strong> how can I convert a <code>factor</code> to a <code>date</code> object without getting <code>NA</code> values. </p>
  
  <p>Here's a similar post: <a href="https://stackoverflow.com/questions/23348992/convert-factor-to-date-time-in-r">Convert Factor to Date/Time in R</a></p>
  
  <p>In that post, the user converted to a <code>character</code> object before a <code>date</code>. I am getting <code>NA</code> values when converting to <code>character</code> object using <code>as.character</code> inside the <code>as.Date</code> function. </p>
  
  <p>I have a column in the dataframe with the date in factor format with different numbers of occurrences. Here's the information contained in the data.frame. </p>
  
  <pre><code>&gt; head(fraud, 5)
    TRANSACTION.DATE TRANSACTION.AMOUNT AIR.TRAVEL.DATE POSTING.DATE
  1 2/27/14                  25.00                 &lt;NA&gt;          2/28/14
  2 2/28/14                  25.00                 &lt;NA&gt;          2/28/14
  3 2/27/14                  25.00                 &lt;NA&gt;          2/28/14
  4 2/27/14                  20.00              2/27/14          2/28/14
  5 2/27/14                  12.13                 &lt;NA&gt;          2/28/14
  
  &gt; str(fraud$TRANSACTION.DATE)
   Factor w/ 519 levels "1/1/14","1/1/15",..: 228 230 228 228 228 230 226 228 230 228 ...
  
  &gt; summary(fraud$TRANSACTION.DATE, 5)
  9/30/14 9/17/14 11/4/14 9/23/14 (Other) 
      197     187     171     160   19221 
  </code></pre>
  
  <p>Converting the factor to a <code>date</code> object resulted in <code>NA</code> values. </p>
  
  <pre><code>&gt; fraud$TRANSACTION.DATE &lt;- as.Date(as.character(fraud$TRANSACTION.DATE), 
  +                                       format = "%m/%d/%Y")
  &gt; head(fraud$TRANSACTION.DATE, 5)
  [1] NA NA NA NA NA
  </code></pre>
  
  <p>Checking if the <code>as.character</code> function worked.</p>
  
  <pre><code>&gt; fraud$TRANSACTION.DATE &lt;- as.character(fraud$TRANSACTION.DATE)
  &gt; head(fraud$TRANSACTION.DATE)
  [1] NA NA NA NA NA NA
  </code></pre>
  
  <p><strong>EDIT: I used as.Date function but got the wrong formatting</strong> </p>
  
  <pre><code>&gt; fraud$TRANSACTION.DATE &lt;- as.Date(fraud$TRANSACTION.DATE, format = "%m/%d/%Y")
  &gt; str(fraud$TRANSACTION.DATE)
   Date[1:19936], format: "0014-02-27" "0014-02-28" "0014-02-27" "0014-02-27" "0014-02-27" ...
  &gt; head(fraud$TRANSACTION.DATE, 5)
  [1] "0014-02-27" "0014-02-28" "0014-02-27" "0014-02-27" "0014-02-27"
  </code></pre>
  
  <p><strong>EDIT 2: Here's the dput value</strong></p>
  
  <pre><code>&gt; dput(droplevels(head(fraud$TRANSACTION.DATE)))
  structure(c(1L, 2L, 1L, 1L, 1L, 2L), .Label = c("2/27/14", "2/28/14"
  ), class = "factor")
  </code></pre>
  
  <p><strong>Solution: using %y instead of %Y</strong></p>
  
  <pre><code>&gt; fraud$TRANSACTION.DATE &lt;- as.Date(fraud$TRANSACTION.DATE, "%m/%d/%y")
  &gt; head(fraud$TRANSACTION.DATE, 5)
  [1] "2014-02-27" "2014-02-28" "2014-02-27" "2014-02-27" "2014-02-27"
  </code></pre>