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.
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.
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
truncatedparameter is non-zeroymd_hmsfunctions 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
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"