通过解析/转换字符串来体验,例如" now-1h","今天"," now-3d","今天+ 30m& #34;在R? 如何识别和转换字符串(例如函数' s参数)到date_time?
答案 0 :(得分:0)
我不建议使用此代码,这很脆弱。这只是为了证明这是可能的:
library(lubridate)
library(stringr)
library(dplyr)
data <- c("now-1h", "today", "now-3d", "today+30m", "-3d")
我们将使用类似于您的输入数据的润滑函数now()
,today()
,days()
,hours()
,...:
str(now())
# POSIXct[1:1], format: "2017-03-24 12:26:18"
str(today())
# Date[1:1], format: "2017-03-24"
str(now() - days(3))
# POSIXct[1:1], format: "2017-03-21 12:27:11"
str(- days(3))
# Formal class 'Period' [package "lubridate"] with 6 slots
# ..@ .Data : num 0
# ..@ year : num 0
# ..@ month : num 0
# ..@ day : num -3
# ..@ hour : num 0
# ..@ minute: num 0
我们将parse()
作为字符串,以便能够实际使用它们,就像那样:
eval(parse(text = "now() + days(3)"))
# [1] "2017-03-27 12:41:34 CEST"
现在让我们用正则表达式解析输入字符串,稍微操作它们以匹配lubridate
语法,然后eval()
uate它们:
res <-
str_match(data, "(today|now)?([+-])?(\\d+)?([dhms])?")[, - 1] %>%
apply(1, function(x) {
time_ <- if (is.na(x[1])) NULL else paste0(x[1], "()")
offset_ <- if (any(is.na(x[2:4]))) NULL else paste(x[2],
recode(x[4], "d" = "days(", "h" = "hours(", "m" = "minutes(", "s" = "seconds("),
x[3],
")")
parse(text = paste(time_, offset_))
}) %>%
lapply(eval)
请注意,您可以将各种类作为输出(POSIXct
或Date
或lubridate::Period
):
invisible(lapply(res, function(x) { print(x) ; str(x) }))
# [1] "2017-03-24 11:57:52 CET"
# POSIXct[1:1], format: "2017-03-24 11:57:52"
# [1] "2017-03-24"
# Date[1:1], format: "2017-03-24"
# [1] "2017-03-21 12:57:52 CET"
# POSIXct[1:1], format: "2017-03-21 12:57:52"
# [1] "2017-03-24 00:30:00 UTC"
# POSIXlt[1:1], format: "2017-03-24 00:30:00"
# [1] "-3d 0H 0M 0S"
# Formal class 'Period' [package "lubridate"] with 6 slots
# ..@ .Data : num 0
# ..@ year : num 0
# ..@ month : num 0
# ..@ day : num -3
# ..@ hour : num 0
# ..@ minute: num 0
(我推荐的是使用生成它的语言预处理数据并拥有适合该作业的工具,似乎是Perl)。