R: extract hour from variable format timestamp
<p>My dataframe has timestamp with and without seconds, and a random use of 0 in front of months and hours, i.e. 01 or 1</p>

<pre><code>library(tidyverse)
df &lt;- data_frame(cust=c('A','A','B','B'), timestamp=c('5/31/2016 1:03:12', '05/25/2016 01:06',
                                           '6/16/2016  01:03', '12/30/2015 23:04:25'))
cust     timestamp
 A      5/31/2016 1:03:12
 A      05/25/2016 01:06
 B      6/16/2016  01:03
 B      12/30/2015 23:04:25
</code></pre>

<p>How to extract hours into a separate column? The desired output:</p>

<pre><code> cust     timestamp            hours
 A      5/31/2016 1:03:12       1
 A      05/25/2016 01:06        1
 B      6/16/2016  9:03         9
 B      12/30/2015 23:04:25    23  
</code></pre>

<p>I prefer the answer with tidyverse and mutate, but my attempt fails to extract hours correctly:</p>

<pre><code>df %&gt;% mutate(hours=strptime(timestamp, '%H') %&gt;% as.character() )

# A tibble: 4 × 3
   cust           timestamp               hours
  &lt;chr&gt;               &lt;chr&gt;               &lt;chr&gt;
1     A   5/31/2016 1:03:12 2016-10-31 05:00:00
2     A    05/25/2016 01:06 2016-10-31 05:00:00
3     B    6/16/2016  01:03 2016-10-31 06:00:00
4     B 12/30/2015 23:04:25 2016-10-31 12:00:00
</code></pre>