转换数据框中的混合日期格式
我已将数据提取到一个数据框中,其中包含需要合并的混合格式日期。数据框的结构如下:
dateDF <- structure(list(id = 1:7, value = c(5813L, 8706L, 4049L, 5877L,
1375L, 2223L, 3423L), date = structure(c(4L, 3L, 2L, 1L, 7L,
6L, 5L), .Label = c("??:?? 05-Dec-11", "??:?? 06-Dec-11", "??:?? 07-Dec-11",
"??:?? 19-Dec-11", "30/12/2011 16:00", "30/12/2011 16:45", "31/12/2011 19:10"
), class = "factor")), .Names = c("id", "value", "date"), row.names = c(NA,
-7L), class = "data.frame")
我使用了 dateDF$date <- str_replace(string=dateDF$date,pattern='\\?\\?\\:\\?\\? ', '12:00 ')
生成:
id value date
1 5813 12:00 19-Dec-11
2 8706 12:00 07-Dec-11
3 4049 12:00 06-Dec-11
4 5877 12:00 05-Dec-11
5 1375 31/12/2011 19:10
6 2223 30/12/2011 16:45
7 3423 30/12/2011 16:00
我现在需要将格式为 hh:mm dd-mmm-yy
的前 4 个样式日期转换为与后三个日期格式一致的格式dd/mm/yyyy hh:mm
对于列中存在第一种格式的每种情况。
任何帮助表示赞赏。
J。
I have extracted data to a data frame with mixed format dates that need consolidated. The structure of the data frame is as follows:
dateDF <- structure(list(id = 1:7, value = c(5813L, 8706L, 4049L, 5877L,
1375L, 2223L, 3423L), date = structure(c(4L, 3L, 2L, 1L, 7L,
6L, 5L), .Label = c("??:?? 05-Dec-11", "??:?? 06-Dec-11", "??:?? 07-Dec-11",
"??:?? 19-Dec-11", "30/12/2011 16:00", "30/12/2011 16:45", "31/12/2011 19:10"
), class = "factor")), .Names = c("id", "value", "date"), row.names = c(NA,
-7L), class = "data.frame")
I have used dateDF$date <- str_replace(string=dateDF$date, pattern='\\?\\?\\:\\?\\? ', '12:00 ')
to produce:
id value date
1 5813 12:00 19-Dec-11
2 8706 12:00 07-Dec-11
3 4049 12:00 06-Dec-11
4 5877 12:00 05-Dec-11
5 1375 31/12/2011 19:10
6 2223 30/12/2011 16:45
7 3423 30/12/2011 16:00
I now need to convert the top 4 style dates with format hh:mm dd-mmm-yy
to a format consistent with the bottom three date formats dd/mm/yyyy hh:mm
for each case where the first format exists in the column.
Any help is appreciated.
J.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道只有这两种格式,则可以首先识别它们(第一个包含字母字符,另一个没有)并进行相应的转换。
If you know that you only have those two formats, you can first identify them (the first one has alphabetic characters in it, the other does not) and convert accordingly.