You could extract the actual year from first four substr
ings, then compare if it is smaller than all
dates of the standardized year starting in the actual year. The standardized year we get from seq
uences with ISOdate
.
standardized_year <- sapply(dat$events, function(x) {
x <- as.POSIXct(x)
y <- as.numeric(substr(x, 1, 4))
ifelse(all(x < seq(ISOdate(y, 7, 1, 0), ISOdate(y + 1, 6, 30, 0), "day")), y - 1, y)
})
dat <- cbind(dat, standardized_year)
dat
# events standardized_year
# 1 2018-02-02 2017
# 2 2010-05-24 2009
# 3 2019-12-30 2019
# 4 2020-07-01 2020
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…