kpratihast
kpratihast

Reputation: 872

How do I extract hour from datetime column which has values in different formats in R?

Some sample values :

12/01/2011 11:49    
18-01-2011 9:50:45

I want the output as:

11   
09

The class of the datetime column is factor in my original dataframe.

Upvotes: 1

Views: 120

Answers (2)

Romain
Romain

Reputation: 21878

You can do this easily with the lubridate package. As pointed out by @alistaire, the default solution does not parse correctly all the information (minutes and seconds) since the formats are inconsistent (one has seconds and the other not). Fortunately, the truncated parameter is here for that. We can set it to 1 since one element is missing.

If truncated parameter is non-zero ymd_hms functions also check for truncated formats.

library(lubridate)

hour(dmy_hms(c("12/01/2011 11:49", "18-01-2011 9:50:45"), truncated = 1))

[1] 11  9

Or even better using pipeline notation %>% from the magrittr package -- love this name.

library(lubridate)    
library(magrittr)

c("12/01/2011 11:49", "18-01-2011 9:50:45") %>%
  dmy_hms(truncated = 1) %>%
  hour()

[1] 11  9

Upvotes: 2

akuiper
akuiper

Reputation: 214927

One option is to use sub with regex:

dt <- c("12/01/2011 11:49", "18-01-2011 9:50:45")

sub(".*\\s(\\d{1,2}):.*", "\\1", as.character(dt))
# [1] "11" "9" 

Or str_extract from stringr:

str_extract(as.character(dt), "(?<=\\s)(\\d{1,2})(?=:)")
# [1] "11" "9" 

Upvotes: 0

Related Questions