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

R, strptime(), %b, trying to convert character to date format

嗨,我有一些关于 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)