如何将 "X hours/days ago" 转换为标准日期时间
How to transform "X hours/days ago" into standard date time
我抓取了新闻数据,包括以下形式的时间:
time <- c("11 hours ago", "2 days ago", "3 days ago")
如何将其转换为标准日期时间格式?顺便说一句:我假设对于日内差异(例如“11 小时前”),浏览器可以识别我的系统时间?由于新闻来自全球各地。
谢谢
润滑剂溶液;
library(lubridate)
time <- c("11 hours ago", "2 days ago", "3 days ago")
time_numeric <- as.numeric(gsub("([0-9]+).*$", "\1", time))
time_logical <- ifelse(grepl('hour',time),'hour',ifelse(grepl('day',time),'day','unnknown'))
time_tidy <- data.frame(diff=time_numeric,type=time_logical)
current_time <- Sys.time()
time_tidy$new <- as_datetime(ifelse(time_tidy$type=='hour',current_time-hours(time_tidy$diff),ifelse(time_tidy$type=='day',current_time-days(time_tidy$diff),current_time)))
time_tidy$new
输出;
[1] "2021-08-30 00:29:21 +03"
[1] "2021-08-28 11:29:21 +03"
[1] "2021-08-27 11:29:21 +03"
如果你只有小时和天作为时间单位,那么,
Sys.time() - ifelse(grepl('hours', time),
as.numeric(gsub('\D+', '', time)) * 3600,
as.numeric(gsub('\D+', '', time)) * 24 * 3600)
#[1] "2021-08-30 00:31:32 +03" "2021-08-28 11:31:32 +03" "2021-08-27 11:31:32 +03"
去掉ago
前面加-
就可以用seq
了。这将适用于 sec
、min
、hour
、day
、DSTday
、week
、month
、quarter
或 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"
要获得向量,请使用 c
和 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"
我抓取了新闻数据,包括以下形式的时间:
time <- c("11 hours ago", "2 days ago", "3 days ago")
如何将其转换为标准日期时间格式?顺便说一句:我假设对于日内差异(例如“11 小时前”),浏览器可以识别我的系统时间?由于新闻来自全球各地。
谢谢
润滑剂溶液;
library(lubridate)
time <- c("11 hours ago", "2 days ago", "3 days ago")
time_numeric <- as.numeric(gsub("([0-9]+).*$", "\1", time))
time_logical <- ifelse(grepl('hour',time),'hour',ifelse(grepl('day',time),'day','unnknown'))
time_tidy <- data.frame(diff=time_numeric,type=time_logical)
current_time <- Sys.time()
time_tidy$new <- as_datetime(ifelse(time_tidy$type=='hour',current_time-hours(time_tidy$diff),ifelse(time_tidy$type=='day',current_time-days(time_tidy$diff),current_time)))
time_tidy$new
输出;
[1] "2021-08-30 00:29:21 +03"
[1] "2021-08-28 11:29:21 +03"
[1] "2021-08-27 11:29:21 +03"
如果你只有小时和天作为时间单位,那么,
Sys.time() - ifelse(grepl('hours', time),
as.numeric(gsub('\D+', '', time)) * 3600,
as.numeric(gsub('\D+', '', time)) * 24 * 3600)
#[1] "2021-08-30 00:31:32 +03" "2021-08-28 11:31:32 +03" "2021-08-27 11:31:32 +03"
去掉ago
前面加-
就可以用seq
了。这将适用于 sec
、min
、hour
、day
、DSTday
、week
、month
、quarter
或 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"
要获得向量,请使用 c
和 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"