as.Date返回NA

时间:2015-03-30 10:10:08

标签: r datetime

以下代码返回NA

as.Date("Thr Jan 15 2015 07:06:04", format = "%a %b %d %Y %T")

已尝试按照?as.Date

中的说明设置系统区域设置
## read in date info in format 'ddmmmyyyy'
## This will give NA(s) in some locales; setting the C locale
## as in the commented lines will overcome this on most systems.
## lct <- Sys.getlocale("LC_TIME"); Sys.setlocale("LC_TIME", "C")
x <- c("1jan1960", "2jan1960", "31mar1960", "30jul1960")
z <- as.Date(x, "%d%b%Y")
## Sys.setlocale("LC_TIME", lct)

1 个答案:

答案 0 :(得分:2)

星期四的缩写是Thu。所以,这可能会起作用

as.Date('Thu Jan 15 2015 07:06:04', format= '%a %b %d %Y %T')
# [1] "2015-01-15"

如果字符串中有typos,请使用sub删除工作日部分

as.Date(sub('[^ ]+ ', '', str1), format= '%b %d %Y %T')
#[1] "2015-01-15"