您好,我在 strptime() 函数方面遇到一些麻烦,我有如下字符数据:
data2[,1]
24-feb-15
26-ene-15
29-dic-14
我尝试使用 srtptime() :
strptime(data2[,1], "%d-%b-%y")
但不幸的是它只适用于 24-feb-15,我猜是因为其他月份是西类牙语缩写,所以 R 无法识别它们,我有很多观察结果,所以我想找到一种方法来做到这一点无需手动更改月份名称。感谢您的帮助。
丹尼尔
最佳答案
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)
关于R,strptime(),%b,尝试将字符转换为日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28785587/