【问题标题】:R, strptime(), %b, trying to convert character to date formatR, strptime(), %b, 试图将字符转换为日期格式
【发布时间】:2015-02-28 18:59:28
【问题描述】:

您好,我在 strptime() 函数方面遇到了一些问题,我有如下字符数据:

data2[,1]

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

我尝试使用 srtptime() :

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

但不幸的是,它只适用于 24-feb-15,我猜是因为其他月份是西班牙语缩写,所以 R 无法识别它们,我有很多观察,所以我想找到一种方法来做到这一点无需手动更改月份的名称。感谢您的帮助。

杰丹尼尔

【问题讨论】:

    标签: r date strptime


    【解决方案1】:

    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)
    

    【讨论】:

    • 另一种方法可能是:dataz[,1]
    猜你喜欢
    • 2023-03-26
    • 2021-03-07
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 2013-12-28
    相关资源
    最近更新 更多