到目前为止我所拥有的是一个数据帧列,其中包含不同字符格式的日期.一些出现在%d.%m.%Y
模式中,一些出现在%m/%d/%Y
:
data$initialDiagnose = as.character(data$initialDiagnose)
data$initialDiagnose[1:10]
[1] "14.01.2009" "9/22/2005" "4/21/2010" "28.01.2010" "09.01.2009" "3/28/2005" "04.01.2005" "04.01.2005" "9/17/2010" "03.01.2010"
Run Code Online (Sandbox Code Playgroud)
我希望它们以一种格式作为Date(),但R当然拒绝.
所以我首先尝试通过分隔符更改它们:
data$initialDiagnose[grep('/', data$initialDiagnose)] = as.character.Date(data$initialDiagnose[grep('/', data$initialDiagnose)], format = '%m/%d/%Y')
Run Code Online (Sandbox Code Playgroud)
类似于'.' 日期.但它没有用.
如何将它们全部更改为一种格式,我可以使用它们?
我有一个日期字符表示的向量,其中格式主要是dmY
(例如27-09-2013),dmy
(例如27-09-13),偶尔也有一些b
或B
几个月.因此,parse_date_time
在lubridate
"允许用户指定多个格式顺序以处理异构日期 - 时间字符表示"的包中对我来说可能是非常有用的功能.
但是,当它们与日期一起出现时,似乎parse_date_time
有解析dmy
日期的问题dmY
.dmy
单独解析,或dmy
与我相关的其他格式一起使用时,它可以正常工作.这种模式也在@ Peyton 在这里回答的评论中注明.建议快速修复,但我想问一下是否有可能处理它lubridate
.
在这里,我展示了一些示例,我尝试在dmy
格式上解析日期和其他一些格式,并相应地指定orders
.
library(lubridate)
# version: lubridate_1.3.0
# regarding how date format is specified in 'orders':
# examples in ?parse_date_time
# parse_date_time(x, "ymd")
# parse_date_time(x, "%y%m%d")
# parse_date_time(x, "%y %m %d")
# these order strings are equivalent and parses the same way
# "Formatting orders might include …
Run Code Online (Sandbox Code Playgroud)