我的矢量日期格式如下:
dates <- c("01AUG2006","01DEC2006","01JUN2006","01MAY2007")
要将此向量转换为类Date
的对象,我编写了此函数:
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
}
使用示例dates
变量:
test <- convert2Date(dates)
[1] "2006-08-01" "2006-12-01" "2006-06-01" "2007-05-01"
class(test)
[1] "Date"
这有效,但看起来有点麻烦,只适用于这种特定格式。由于这可能是一个常见问题,因此必须有一种更简单,更通用的方法。有人建议吗?非常感谢!
答案 0 :(得分:7)
看起来好像是
as.Date(dates,format="%d%b%Y")
工作正常吗?
[1] "2006-08-01" "2006-12-01" "2006-06-01" "2007-05-01"
答案 1 :(得分:5)
问题是你的语言环境。就像我一样。
我的系统是葡萄牙语
> 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.
如果我将月份更改为葡萄牙语abrev表单,则无法重置语言环境,lubridate::dmy
将对我有效。
dmy(c("01AGO2006","01DEZ2006","01JUN2006","01MAI2007"))
[1] "2006-08-01 UTC" "2006-12-01 UTC" "2006-06-01 UTC" "2007-05-01 UTC"
或者从dmy函数调用设置语言环境,结果更容易。
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"
现在没关系。
对于非英语系统,必须相应地更改语言环境或重写文本。
将区域设置重置为美国英语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
现在同样有效
as.Date(dates,format="%d%b%Y")
# [1] "2006-08-01" "2006-12-01" "2006-06-01" "2007-05-01"
答案 2 :(得分:2)
这还涉及解析子字符串,但它更短并且不需要设置语言环境:
as.Date(paste(substr(dates, 6, 9), # year
match(substr(dates, 3, 5), toupper(month.abb)), # month
substr(dates, 1, 2), # day
sep = "-"))