0

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.

Ben.R
  • 3
  • 3
  • See the linked answer. And for more on formats see `?strptime`. There is an extensive list in the help. – Pierre L Feb 24 '16 at 19:39

2 Answers2

1

You first need to parse the time

d = strptime(date1,format = '%d.%m.%Y %H:%M:%S')

Then use lubridate to extract parts like hour etc.

library(lubridate)
hour(d)
minute(d)
Pekka
  • 2,348
  • 2
  • 21
  • 33
1

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")
Pierre L
  • 28,203
  • 6
  • 47
  • 69
RHertel
  • 23,412
  • 5
  • 38
  • 64