I am trying to extract the hour (only) from a variable that has date and time. There are several questions explaining how to extract
df$hour <- format(as.POSIXct(citistation$starttime, format="%m/%d/%Y %H:%M:%S"), format="%H")
date <- c("1/1/2013 0:01","12/31/2013 21:49:19")
We can use
sub. Match one or more non-white space (
\\S+) followed by one or more space (
\\s+), capture the one or more characters that are not a
([^:]+)) followed by a
: and characters until the end of the string, replace it with the the backreference (
\\1) of the capture group.
sub("\\S+\\s+([^:]+):.*", "\\1", date) # "0" "21"
It is better to convert to 'DateTime' class and extract the
library(lubridate) hour(parse_date_time(date, c('mdy_HM', 'mdy_HMS'))) # 0 21
date <- c('1/1/2013 0:01','12/31/2013 21:49:19')