Search code examples
rgethour

How to extract the hour of this kind of timestamp?


This is how my timestamp looks like

date1 = timestamp[1]

date1
[1] 07.01.2016 11:52:35
3455 Levels: 01.02.2016 00:11:52 01.02.2016 00:23:35 01.000:30:21    31.01.2016 23:16:18

When I try to extract the hour, I get an invalid 'trim' argument. Thank you !

 format(date1, "%I")

 Error in format.default(structure(as.character(x), names = names(x), dim = dim(x),  : 

invalid 'trim' argument

How can I extract single components like the hour from this timestamp.


Solution

  • With base R:

    format(strptime(x,format = '%d.%m.%Y %H:%M:%S'), "%H")
    [1] "11"
    

    data

    x <- as.factor("07.01.2016 11:52:35")