我使用的是一种旧的日期格式,前两位数字代表20世纪的年份。小数点后的前两位为月份(即01-12),小数点后第三位和第四位为当天(即01-31)。问题是,以零结尾的日期(即10、20、30)会去掉后面的零,因此某些日期格式只有三位小数。
如何恢复尾随的零,以便将旧的日期格式转换为现代日期格式(即yyyy-mm-dd)?
示例:
library(tidyverse)
# Example data
xx <- data.frame(yr_mo_da = c('89.1208','89.1209','89.121', '89.1211'))
# My attempt to extract year, month, day from the old date format
# NOTE how the day is not extracted properly because there is not a fourth decimal place on Dec. 10
xx$year <- as.numeric(gsub("\\..*","",xx$yr_mo_da))
xx$month <- as.numeric(sub("^[0-9]+\\.([0-9]{2}).*", "\\1", xx$yr_mo_da))
xx$day <- as.numeric(sub("^[0-9]+\\.[0-9]{2}([0-9]{2}).*", "\\1", xx$yr_mo_da))
xx
#> yr_mo_da year month day
#> 1 89.1208 89 12 8.000
#> 2 89.1209 89 12 9.000
#> 3 89.121 89 12 89.121
#> 4 89.1211 89 12 11.000
# Create modern date format
xx <- xx %>%
mutate(year = year + 1900,
Date = make_date(year,month,day)) %>%
select(-c(yr_mo_da,year,day,month))
xx
#> Date
#> 1 1989-12-08
#> 2 1989-12-09
#> 3 <NA>
#> 4 1989-12-11
正确的输出应该如下所示:
xx
#> Date
#> 1 1989-12-08
#> 2 1989-12-09
#> 3 1989-12-10
#> 4 1989-12-11
创建于2023-09-05
reprex v2.0.2