使用R语言的as.Date进行日期转换时,需要注意本地设置(locale setting),否则有可能转换失败。
> as.Date("01jul2020", format = "%d%b%Y")
[1] NA
> lc <- Sys.getlocale("LC_TIME") # get the LC_TIME and save it
> Sys.setlocale("LC_TIME", "C") # set to C lang default
[1] "C"
> as.Date("01jul2020", format = "%d%b%Y")
[1] "2020-07-01"
> Sys.setlocale("LC_TIME", lc) # restore the locale setting
[1] "Chinese (Simplified)_China.936"
注: “C” 是C语言的默认设置,也就是美国的locale设置。