0

Consider the following data

my_date <- "October 15, 2018"

I want to convert it into a date.

Thus, I tried to use the as.Date function like below

as.Date(my_date, format = "%B %d, %Y")

But, this returns just a NA and I cannot understand what is the problem.

How can I fix this problem?

  • 3
    You probably have a non-English locale hence R is not able to identify months in English. You may try to change the locale https://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r – Ronak Shah Jul 24 '23 at 10:39
  • @RonakShah Great! Thank you for the comment! – MinChul Park Jul 24 '23 at 10:44

1 Answers1

2

Depending on your use case you may or may not want to alter your locales. If the latter applies, lubridate parsers allow you to set locale just for the function, without changing your environment:

lubridate::mdy("October 15, 2018", locale = "C")
#> [1] "2018-10-15"

Or if it's part of data import, perhaps consider readr, its default locale handling is quite convenient on non-US systems:

library(readr)

# system lcoale:
Sys.getlocale()
#> [1] "LC_COLLATE=Estonian_Estonia.utf8;LC_CTYPE=Estonian_Estonia.utf8;LC_MONETARY=Estonian_Estonia.utf8;LC_NUMERIC=C;LC_TIME=Estonian_Estonia.utf8"

# E.g. for "%B %d, %Y" to work we would need "juuli" instead of "July":
format(Sys.time(), "%B %d, %Y")
#> [1] "juuli 24, 2023"

# readr::default_locale(), will be used by readr::parse_date() :
default_locale()
#> <locale>
#> Numbers:  123,456.78
#> Formats:  %AD / %AT
#> Timezone: UTC
#> Encoding: UTF-8
#> <date_names>
#> Days:   Sunday (Sun), Monday (Mon), Tuesday (Tue), Wednesday (Wed), Thursday
#>         (Thu), Friday (Fri), Saturday (Sat)
#> Months: January (Jan), February (Feb), March (Mar), April (Apr), May (May),
#>         June (Jun), July (Jul), August (Aug), September (Sep), October
#>         (Oct), November (Nov), December (Dec)
#> AM/PM:  AM/PM

# import CSV that uses foreign date format for that specific system locale, 
# without changing any locale settings:

dummy_csv <- '"October 15, 2018"';
read_csv(I(dummy_csv), col_names = FALSE, col_types = cols(col_date(format = "%B %d, %Y")))
#> # A tibble: 1 × 1
#>   X1        
#>   <date>    
#> 1 2018-10-15
margusl
  • 7,804
  • 2
  • 16
  • 20