将文本中包含的半年格式化为日期

Format year half contained in text as dates

我在文本中包含日期值,每个日期值包含半年:

date_by_half <- c("2016 H1", "2017 H2", "2018 H1")

我想从文本中提取日期并将其存储为每半场的第一天或 "semester"。所以,像这样:

ysemester(date_by_half)
#[1] "2016-01-01" "2017-07-01" "2018-01-01"

我熟悉 lubridate::yq() 功能,但我发现这只适用于宿舍。

lubridate::yq(date_by_half)
#[1] "2016-01-01" "2017-04-01" "2018-01-01"

现在我的解决方法是用 Q3 替换 H2:

lubridate::yq(stringr::str_replace(date_by_half,"H2", "Q3"))
#[1] "2016-01-01" "2017-07-01" "2018-01-01"

但是,我想知道是否有更多 eloquent 解决方案使用 lubridate(或其他一些快速且可重复使用的方法)。

您可以创建自己的函数来实现这一目的。

# Your data
date_by_half <- c("2016 H1", "2017 H2", "2018 H1")

# Function to do the work
year_dater <- function(dates) {
  year <- substr(dates, 1, 4)
  quarter <- substr(dates, 6, 7)
  month <- ifelse(quarter=="H1", 1, 7) 
  dates <- paste0(year, "-", month, "-", rep(1, length(month)))

  return(dates)
}

# Running the function
dates <- year_dater(date_by_half)

# As date format
as.POSIXct(dates)
"2016-01-01 CET"  "2017-07-01 CEST" "2018-01-01 CET" 

我们可以使用 lubridate 中的 ceiling_date 函数,单位为 "halfyear",change_on_boundary 参数设置为 FALSE,这样边界上的日期 (2018 -01-01、2017-07-01 等)永远不会与 yq 函数一起四舍五入。

library(lubridate)
ceiling_date(yq(date_by_half), unit = "halfyear", change_on_boundary = FALSE)
#[1] "2016-01-01" "2017-07-01" "2018-01-01"

一个班轮

这些单线仅使用基础 R:

1) 阅读。table/ISOdate

with(read.table(text = date_by_half), as.Date(ISOdate(V1, ifelse(V2=="H1",1,7), 1)))
## [1] "2016-01-01" "2017-07-01" "2018-01-01"

2) sub 更短的是:

as.Date(sub(" H2", "-7-1", sub(" H1", "-1-1", date_by_half)))
## [1] "2016-01-01" "2017-07-01" "2018-01-01"

S3

另一种方法是为半年日期创建 S3 class、"half"。我们只会实现我们需要的方法。

as.half <- function(x, ...) UseMethod("as.half")

as.half.character <- function(x, ...) {
  year <- as.numeric(sub("\D.*", "", x))
  half <- as.numeric(sub(".*\D", "", x))
  structure(year + (half - 1)/2, class = "half")
}

as.Date.half <- function(x, ...) {
  as.Date(ISOdate(as.integer(x), 12 * (x - as.integer(x)) + 1, 1))
}

# test

as.Date(as.half(date_by_half))
## [1] "2016-01-01" "2017-07-01" "2018-01-01"