It is convenient to use separate
from tidyr
if we need to split
by 'Date' and 'Time'
library(tidyr)
df2 %>%
separate(DateTime, into = c("Date", "Time"), sep=" ")
If we need to extract the year, use the gsub
df2$Year <- gsub(".*/|\\s+.*", "", df2$DateTime)
Or with as.POSIXct
as it is 'DateTime'
df2$Year <- format(as.POSIXct(df2$DateTime, format = "%m/%d/%Y %H:%M:%S"), "%Y")
Or as @Jaap suggested in the comments
strptime(df2$DateTime, format = '%m/%d/%Y %I:%M:%S %p')$year + 1900
The difference between as.POSIXct
and strptime
is the class
of the output object. With as.POSIXct
, it has POSIXct
class, while strptime
is POSIXlt
and POSIXct
. In addition to the above, there are convenience in using either one. If somebody wants to extract different pieces like 'month', 'year' etc, strptime
is more convenient. With as.POSIXct
, we can wrap format
to extract the 'month', 'year'. Also, note that strptime
class have some clash in using with dplyr
functions
Or use lubridate
library(lubridate)
df2$Year <- year(mdy_hms(df2$DateTime))
NOTE: It is better to use Date Time functions for extracting than with string manipulation