0

I have a character vector

x=c("Thu Jan 30 2020")

when I use function as.Date in base package as follows, it returns NA.

as.Date(x,format="%a %b %d %Y")

What am I doing wrong?

wkde
  • 453
  • 3
  • 13
  • 3
    works for me. Check your locale: %a and %b are **locale-specific**. What is the result of `Sys.getlocale()` (specifically `Sys.getlocale("LC_TIME")` ?) – Ben Bolker Jun 07 '21 at 03:55
  • 3
    You need to change the R/R Studio to English. To do that try https://stackoverflow.com/questions/12760491/the-r-console-is-in-my-native-language-how-can-i-set-r-to-english or https://stackoverflow.com/questions/13575180/how-to-change-language-settings-in-r – Ronak Shah Jun 07 '21 at 03:56
  • I got> Sys.getlocale("LC_TIME") [1] "Korean_Korea.949" and solved it by > Sys.setlocale("LC_ALL","English") Thanks so much! – wkde Jun 07 '21 at 04:22

1 Answers1

0

A hard-coding workaround

s <- unlist(strsplit(x, " "))[c(4, 2, 3)]
as.Date(paste0(
  replace(
    s,
    2,
    sprintf("%02d", match(s[2], month.abb))
  ),
  collapse = "-"
))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81