将字符转换为日期 [英] Converting character to Date

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

问题描述

我有一个载体,日期格式如下:

I have a vector with dates in the following format:

 dates <- c("01AUG2006","01DEC2006","01JUN2006","01MAY2007")

将此向量转换为类日期,我写了这个函数:

To convert this vector to an object of class Date, I wrote this function:

convert2Date <- function(x) {
  require(car)
  d <- substr(x,0,2)
  m.text <- substr(x,3,5)
  m <- Recode(m.text, "'JAN'=1 ;'FEB'=2;'MAR'=3;'APR'=4;'MAY'=5;'JUN'=6; 
                       'JUL'=7;'AUG'=8;'SEP'=9;'OCT'=10;'NOV'=11;'DEC'=12")
  y <- substr(x,6,9)
  out <- as.Date(paste(d,m,y,sep="/"),"%d/%m/%Y")
  out
}

使用示例日期变量:

test <- convert2Date(dates)
[1] "2006-08-01" "2006-12-01" "2006-06-01" "2007-05-01"
class(test)
[1] "Date"

这个w orks,但它看起来有点麻烦,只适用于这种特定的格式。由于这可能是一个常见的问题,所以必须有一个更简单和更通用的方法。任何建议任何人?非常感谢!

This works, but it looks somewhat cumbersome and only works for this specific format. Since this is probably a common problem, there must be an simpler and more versatile way of doing this. Any suggestions anyone? Many thanks!

推荐答案

问题是您的区域设置。就像我的一样。

The problem is your locale. Just like mine.

我的系统是葡萄牙语

> sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252  LC_CTYPE=Portuguese_Portugal.1252    LC_MONETARY=Portuguese_Portugal.1252
[4] LC_NUMERIC=C                         LC_TIME=Portuguese_Portugal.1252    

如果我只是尝试

library(lubridate)
dmy(c("01AUG2006","01DEC2006","01JUN2006","01MAY2007"))
[1] NA               NA               "2006-06-01 UTC" NA              
Warning message:
 3 failed to parse.

没有区域设置重置, lubridate :: dmy 如果我将月份更改为葡萄牙语abrev表单,将为我工作。

Without locale resetting, lubridate::dmy will work for me if I change Months to the Portuguese abrev form.

dmy(c("01AGO2006","01DEZ2006","01JUN2006","01MAI2007"))
[1] "2006-08-01 UTC" "2006-12-01 UTC" "2006-06-01 UTC" "2007-05-01 UTC"

或从dmy函数调用中设置语言环境,其结果变得相当简单。

OR set locale from dmy function call, which turn out to be pretty easier.

dmy(dates, locale = "English_United States.1252") # Without resetting locale
[1] "2006-08-01 UTC" "2006-12-01 UTC" "2006-06-01 UTC" "2007-05-01 UTC"



<

It is OK now.

对于非英语系统,语言环境必须相应更改或文本重写。

for non-English systems, locale must be changed accordingly or text rewritten.

将语言环境重置为美国英语2252

Resetting locale to US English 2252

Sys.setlocale(category = "LC_ALL", locale = "English_United States.1252")
dates <- c("01AUG2006","01DEC2006","01JUN2006","01MAY2007")
dmy(dates)
[1] "2006-08-01 UTC" "2006-12-01 UTC" "2006-06-01 UTC" "2007-05-01 UTC"

在重新设置语言环境后, Date 现在使用相同的工作

The same works now for Date after resetting locale

as.Date(dates,format="%d%b%Y")
# [1] "2006-08-01" "2006-12-01" "2006-06-01" "2007-05-01"

这篇关于将字符转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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