title: How to convert in both directions between year,month,day and dates in R?
body: <p>How to convert between year,month,day and dates in R?</p>
  
  <p>I know one can do this via strings, but I would prefer to avoid converting to strings, partly because maybe there is a performance hit?, and partly because I worry about regionalization issues, where some of the world uses "year-month-day" and some uses "year-day-month".</p>
  
  <p>It looks like ISODate provides the direction year,month,day -> DateTime , although it does first converts the number to a string, so if there is a way that doesn't go via a string then I prefer.</p>
  
  <p>I couldn't find anything that goes the other way, from datetimes to numerical values?  I would prefer not needing to use strsplit or things like that.</p>
  
  <p>Edit: just to be clear, what I have is, a data frame which looks like:</p>
  
  <pre><code>year month day hour somevalue
  2004 1     1   1   1515353
  2004 1     1   2   3513535
  ....
  </code></pre>
  
  <p>I want to be able to freely convert to this format:</p>
  
  <pre><code>time(hour units) somevalue
  1             1515353
  2             3513535
  ....
  </code></pre>
  
  <p>... and also be able to go back again.</p>
  
  <p>Edit: to clear up some confusion on what 'time' (hour units) means, ultimately what I did was, and using information from <a href="https://stackoverflow.com/questions/12977073/how-to-find-the-difference-between-two-dates-in-hours-in-r">How to find the difference between two dates in hours in R?</a>:</p>
  
  <p><strong>forwards direction:</strong></p>
  
  <pre><code>lh$time &lt;- as.numeric( difftime(ISOdate(lh$year,lh$month,lh$day,lh$hour), ISOdate(2004,1,1,0), units="hours"))
  lh$year &lt;- NULL; lh$month &lt;- NULL; lh$day &lt;- NULL; lh$hour &lt;- NULL
  </code></pre>
  
  <p><strong>backwards direction:</strong></p>
  
  <p>... well, I didnt do backwards yet, but I imagine something like:</p>
  
  <ul>
  <li>create difftime object out of lh$time (somehow...)</li>
  <li>add ISOdate(2004,1,1,0) to difftime object</li>
  <li>use one of the solution below to get the year,month,day, hour back</li>
  </ul>
  
  <p>I suppose in the future, I could ask the exact problem I'm trying to solve, but I was trying to factorize my specific problem into generic reusable questions, but maybe that was a mistake?</p>