I have scraped news data, including time in form of:
time <- c("11 hours ago", "2 days ago", "3 days ago")
How can I translate this into a standard date time format? BTW: I assume that for intra-day differences (e.g. "11 hours ago") the browser recognizes my system time? Since news come from around the globe.
You can use seq
when removing the ago
and adding in front a -
. This will work for times given as sec
, min
, hour
, day
, DSTday
, week
, month
, quarter
or year
.
lapply(sub(" ago", "", time), function(x) seq(Sys.time(), by=paste0("-", x),
length.out = 2)[2])
#[[1]]
#[1] "2021-08-29 23:41:26 CEST"
#
#[[2]]
#[1] "2021-08-28 10:41:26 CEST"
#
#[[3]]
#[1] "2021-08-27 10:41:26 CEST"
To get a vector use c
with do.call
:
do.call(c, lapply(sub(" ago", "", time), function(x) seq(Sys.time(),
by=paste0("-",x), length.out = 2)[2]))
#[1] "2021-08-30 00:11:15 CEST" "2021-08-28 11:11:15 CEST"
#[3] "2021-08-27 11:11:15 CEST"