as.Date yields NA for month name “März” (march)

前端 未结 3 857
挽巷
挽巷 2021-01-20 05:46

I got a scraped character vector with dates. My problem: When using as.Date(), every date containing the month name \"März\" (= which means \"march\" in German)

3条回答
  •  醉话见心
    2021-01-20 06:31

    I also had a quite similar issue. I'm going to write the solution I found hoping to help users with Italian local system setting

     Sys.setlocale("LC_TIME")
    

    [1] "Italian_Italy.1252"

    and I had to convert factors to date: factors were

    levels(dates)
    

    [1] "1. Jun. 2012" "11. Sep. 2012" "19. Oct. 2012" "20. Mar. 2013" "28. Jun. 2012" [6] "7. May. 2012"

    This produced NA in the conversion for all months but March (because the abbreviation is the same in Italian)

     head(as.Date(dates, format= "%d. %b. %Y"))
    

    [1] NA NA NA NA NA NA

     summary(GEM_variability$date)
    
        Min.      1st Qu.       Median         Mean      3rd Qu.         Max. 
    

    "2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20" "2013-03-20" NA's "559"

    I found the solution in the help file of ?strftime

    lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
    dates<- as.Date(date, format="%d. %b. %Y")
    #dates<- strptime(date, format="%d. %b. %Y")
    Sys.setlocale("LC_TIME", lct)
    

提交回复
热议问题