我有一组日期如下:
example_dates = c("October 12th 2018","September 16th 2018","September 26th 2018")
玩了一段时间,但仍然不知道如何将其转换为R中的Date类。有人知道这种问题的简单解决方案/包吗?
答案 0 :(得分:5)
这是lubridate
的Tidyverse解决方案:
library(lubridate)
example_dates = c("October 12th 2018","September 16th 2018","September 26th 2018")
lubridate::mdy(example_dates)
输出:
[1] "2018-10-12" "2018-09-16" "2018-09-26"
答案 1 :(得分:2)
尝试将sub
与as.Date
一起使用:
example_dates = c("October 12th 2018","September 16th 2018","September 26th 2018")
example_dates <- sub("(?<= \\d{2})\\D{2}(?= \\d{4})", "", example_dates, perl=TRUE)
dates <- as.Date(example_dates, format = "%B %d %Y")
dates
[1] "2018-10-12" "2018-09-16" "2018-09-26"