Can I apply a function over a vector using base tryCatch?

后端 未结 3 698
既然无缘
既然无缘 2021-01-16 16:24

I\'m trying to parse dates (using lubridate functions) from a vector which has mixed date formats.

departureDate <- c(\"Aug 17, 2020 12:00:00 AM\", \"Nov          


        
3条回答
  •  攒了一身酷
    2021-01-16 17:16

    We can use lubridate::parse_date_time which can take multiple formats.

    lubridate::parse_date_time(departureDate, c('%b %d, %Y %I:%M:%S %p', '%d/%m/%Y'))
    
    #[1] "2020-08-17 UTC" "2019-11-19 UTC" "2020-12-21 UTC" "2020-12-24 UTC"
    #[5] "2020-12-24 UTC" "2020-04-19 UTC" "2019-06-28 UTC" "2019-08-16 UTC"
    #[9] "2019-02-04 UTC" "2019-04-10 UTC" "2019-07-28 UTC" "2019-07-26 UTC"
    #[13] "2020-06-22 UTC" "2020-04-05 UTC" "2021-05-01 UTC"
    

    Since in departureDate month name is in English, you need the locale to be English as well.

    Refer How to change the locale of R? if you have non-English locale.

提交回复
热议问题