R,strptime(),%b,尝试将字符转换为日期格式

时间:2015-02-28 18:59:29

标签: r date strptime

嗨,我有一些关于strptime()函数的麻烦,我有像:

这样的字符数据
data2[,1]

24-feb-15
26-ene-15
29-dic-14

我尝试使用srtptime():

strptime(data2[,1], "%d-%b-%y")

但不幸的是它只适用于24-feb-15,我猜它是因为其他月份是西班牙语缩写,所以R不认识它们,我有很多观察所以我想找到一种方法在不手动更改月份名称的情况下执行此操作。谢谢你的帮助。

JDaniel

1 个答案:

答案 0 :(得分:3)

strptime将识别当前语言环境中的缩写名称。您可以将当前区域设置更改为西班牙语,转换日期,然后将其更改回原始设置:

#save your current locale
original_locale<-Sys.getlocale(category = "LC_TIME")

#change it to spanish
Sys.setlocale(category = "LC_TIME", locale = "es_ES.UTF-8")

#transform your dates
data<-c("24-feb-15","26-ene-15","29-dic-14")
strptime(data,format="%d-%b-%y")

#[1] "2015-02-24 GMT" "2015-01-26 GMT" "2014-12-29 GMT"

#change it back to the original setting
Sys.setlocale(category = "LC_TIME", locale = original_locale)
相关问题