使用lubridate在R中解析多种格式的日期 [英] Parsing dates in multiple formats in R using lubridate

查看:37
本文介绍了使用lubridate在R中解析多种格式的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期为MM/DD/YY HH:MM的数据,其他日期为普通的MM/DD/YY格式.我想将它们全部解析为与"2010-12-01 12:12 EST"相同的格式.我应该怎么做呢?我尝试了以下ifelse语句,它给了我很多长整数,并告诉我大量数据点解析失败:

I have data with dates in MM/DD/YY HH:MM format and others in plain old MM/DD/YY format. I want to parse all of them into the same format as "2010-12-01 12:12 EST." How should I go about doing that? I tried the following ifelse statement and it gave me a bunch of long integers and told me a large number of my data points failed to parse:

df_prime$date <- ifelse(!is.na(mdy_hm(df$date)), mdy_hm(df$date), mdy(df$date))

df_prime是我最初加载的数据帧 df 的副本

df_prime is a duplicate of the data frame df that I initially loaded in

  IEN          date admission_number KEY_PTF_45       admission_from                        discharge_to
1  12  3/3/07 18:05                1     252186         OTHER DIRECT                                
2  12  3/9/07 12:10                1     252186                      RETURN TO COMMUNITY-    INDEPENDENT
3  12 3/10/07 15:08                2     252382 OUTPATIENT TREATMENT                                
4  12 3/14/07 10:26                2     252382                      RETURN TO COMMUNITY-INDEPENDENT
5  12 4/24/07 19:45                3     254343         OTHER DIRECT                                
6  12 4/28/07 11:45                3     254343                      RETURN TO COMMUNITY-INDEPENDENT
...
1046334 23613488506       2/25/14               NA         NA                            
1046335 23613488506 2/25/14 11:27               NA         NA                            
1046336 23613488506       2/28/14               NA         NA                            
1046337 23613488506        3/4/14               NA         NA                            
1046338 23613488506 3/10/14 11:30               NA         NA                            
1046339 23613488506 3/10/14 12:32               NA         NA        

很抱歉,如果某些格式不正确,但日期列是最重要的格式.

Sorry if some of the formatting isn't right, but the date column is the most important one.

以下是通过 dput 命令提供的部分数据框代码:

Below is some code for a portion of my data frame via a dput command:

structure(list(IEN = c(23613488506, 23613488506, 23613488506, 23613488506, 23613488506, 23613488506), date = c("2/25/14", "2/25/14 11:27", "2/28/14", "3/4/14", "3/10/14 11:30", "3/10/14 12:32")), .Names = c("IEN", "date"), row.names = 1046334:1046339, class = "data.frame") 

推荐答案

lubridate 包的 mdy_hm 具有一个 truncated 参数,可让您提供可能没有全部内容的日期.例如:

The lubridate package's mdy_hm has a truncated parameter that lets you supply dates that might not have all the bits. For your example:

> mdy_hm(d$date,truncated=2)
[1] "2014-02-25 00:00:00 UTC" "2014-02-25 11:27:00 UTC"
[3] "2014-02-28 00:00:00 UTC" "2014-03-04 00:00:00 UTC"
[5] "2014-03-10 11:30:00 UTC" "2014-03-10 12:32:00 UTC"

这篇关于使用lubridate在R中解析多种格式的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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