title: Why do `as` methods remove vector names, and is there a way around it?
body: <p>Basically, I'm trying to keep a vector named <code>dates</code> of special <code>Date</code>s that come up a lot in my analysis, say New Year's 2016 and July 4 2015. I want to be able to extract from this by name instead of index for robustness, e.g., <code>dates["nyd"]</code> to get New Year's and <code>dates["ind"]</code> to get July 4.</p>
  
  <p>I thought this would be simple:</p>
  
  <pre><code>dates &lt;- as.Date(c(ind = "2015-07-04", nyd = "2016-01-01"))
  </code></pre>
  
  <p>But <code>as.Date</code> has stripped the names:</p>
  
  <pre><code>dates
  # [1] "2015-07-04" "2016-01-01"
  </code></pre>
  
  <p>It's not like <code>Date</code> vectors can't be named (which would be strange, given they're basically specifically-interpreted <code>integer</code>s):</p>
  
  <pre><code>setNames(dates, c("ind", "nyd"))
  #          ind          nyd 
  # "2015-07-04" "2016-01-01" 
  </code></pre>
  
  <p>And unfortunately there's no way to declare a <code>Date</code> vector directly (as far as I know?), especially without knowing the underlying integer values of the dates.</p>
  
  <p>Exploring this, it seems this is standard practice for the <code>as*</code> class of functions:</p>
  
  <pre><code>as.integer(c(a = "123", b = "436"))
  # [1] 123 436
  
  as(c(a = 1, b = 2), "character")
  # [1] "1" "2"
  </code></pre>
  
  <p>Is there a reason why this is the case? The loss of names isn't mentioned in <code>?as</code> or any of the other help pages I've seen.</p>
  
  <p>More generally, is there a way (using something other than <code>as*</code>) to ensure the names of an object are not lost in a conversion?</p>
  
  <p>Of course one approach is to write custom functions like <code>as.Date.named</code> or create a custom class <code>as.named</code> with associated methods, but it would be surprising to me if there wasn't something like this already in place, as it seems like this should be a pretty common operation.</p>
  
  <p>In case it matters, I'm on 3.2.2.</p>