R,strptime(),%b,尝试将字符转换为日期格式 [英] R, strptime(), %b, trying to convert character to date format
问题描述
我在使用strptime()函数时遇到了一些麻烦,我的字符数据如下:
Hi i have some troubles concerning the strptime() function, i have character data like:
data2[,1]
24-feb-15
26-ene-15
29-dic-14
我尝试使用srtptime():
i try to use srtptime() :
strptime(data2[,1], "%d-%b-%y")
但是不幸的是,它仅适用于15年2月24日,我想是因为其他月份都是西班牙语的缩写,所以R无法识别它们,我有很多观察结果,所以我想找到一种方法来做无需手动更改月份名称.感谢您的帮助.
but unfortunately it only works for 24-feb-15, i guess its because the other months are spanish abbreviated, so R doesn't recognize them, i have a lot of observations so i want to find a way to do it without changing manually the name of the months. Thanks for your help.
JDaniel
推荐答案
strptime
将识别当前语言环境中的缩写名称.您可以将当前语言环境更改为西班牙语,转换日期,然后将其更改回原始设置:
strptime
will recognize abbreviated names in the current locale. You can change your current locale to spanish, transform your dates, then change it back to the original setting:
#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)
这篇关于R,strptime(),%b,尝试将字符转换为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!