将具有混合日期格式的变量转换为一种格式 [英] Convert variable with mixed date formats to one format

查看:150
本文介绍了将具有混合日期格式的变量转换为一种格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  date 
1 1987年2月25日
2 1974年8月20日
3 1984年10月9日
4 1992年8月18日
5 1995年9月19日
6 16年10月16日
7 30年9月30日
8 22 2008年1月
9 13-11-1961
10 1987年8月18日
11 15年9月15日
12 1994年10月5日
13 1984年12月5日
14 03/23/87
15 1988年8月30日
16 26-10-1993
17 1989年8月22日
18 1997年9月13日

我有一个很大的数据框,它有一个日期变量的日期变量。变量中的大部分格式如上所示 - 还有一些非常罕见的其他格式。为什么有多种格式的原因是,数据从各个网站,每个使用不同的格式拉在一起。



我曾尝试使用直接转换例如

  strftime(mydf $ date,%d /%m /%Y)

但是如果有多种格式,这种转换将不起作用。我不想求助于多个gsub类型的编辑。我想知道是否我错过了一个更简单的解决方案?



代码例如:

 < (c)(12L,8L,18L,6L,7L,4L,14L,
10L,1L,5L,3L,17L,16L,11L,15L,13L) ,9L,2L),.Label = c(13-11-1961,
97年9月13日,15-Sep-70,16-Oct-63,8月18日1987年8月18日,b $ b1995年9月19日,1974年8月20日,1989年8月22日,2008年1月22日,b $ b03/23/87, 1987年2月25日,1993年10月26日,1993年9月30日,1988年8月30日,
1984年12月5日,1994年10月5日 class =factor)),.Names =date,row.names = c(NA,
-18L),class =data.frame)


解决方案

您可以在包<$ c中尝试 parse_date_time $ c> lubridate 使用 orders 参数允许用户指定几个格式顺序来处理不同的日期时间字符表示。就像...

pre $ library $($)
parse_date_time(x = df $ date
= c(dmy,d BY,m / d / y),
locale =eng)

...应该能够处理大部分格式。请注意, b / B 格式为 locale 敏感

A sample of my dataframe:

                date
1   25 February 1987
2     20 August 1974
3     9 October 1984
4     18 August 1992
5  19 September 1995
6          16-Oct-63
7          30-Sep-65
8        22 Jan 2008
9         13-11-1961
10    18 August 1987
11         15-Sep-70
12    5 October 1994
13   5 December 1984
14          03/23/87
15    30 August 1988
16        26-10-1993
17    22 August 1989
18         13-Sep-97

I have a large dataframe with a date variable that has multiple formats for dates. Most of the formats in the variable are shown above- there are a couple of very rare others too. The reason why there are multiple formats is that the data were pulled together from various websites that each used different formats.

I have tried using straightforward conversions e.g.

strftime(mydf$date,"%d/%m/%Y")

but these sorts of conversion will not work if there are multiple formats. I don't want to resort to multiple gsub type editing. I was wondering if I am missing a more simple solution?

Code for example:

    structure(list(date = structure(c(12L, 8L, 18L, 6L, 7L, 4L, 14L, 
10L, 1L, 5L, 3L, 17L, 16L, 11L, 15L, 13L, 9L, 2L), .Label = c("13-11-1961", 
"13-Sep-97", "15-Sep-70", "16-Oct-63", "18 August 1987", "18 August 1992", 
"19 September 1995", "20 August 1974", "22 August 1989", "22 Jan 2008", 
"03/23/87", "25 February 1987", "26-10-1993", "30-Sep-65", "30 August 1988", 
"5 December 1984", "5 October 1994", "9 October 1984"), class = "factor")), .Names = "date", row.names = c(NA, 
-18L), class = "data.frame")

解决方案

You may try parse_date_time in package lubridate which "allows the user to specify several format-orders to handle heterogeneous date-time character representations" using the orders argument. Something like...

library(lubridate)
parse_date_time(x = df$date,
                orders = c("d m y", "d B Y", "m/d/y"),
                locale = "eng")

...should be able to handle most of your formats. Please note that b/B formats are locale sensitive.

这篇关于将具有混合日期格式的变量转换为一种格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆