为什么strptime总是返回NA?

时间:2014-10-02 15:48:40

标签: r strptime

retest = c("May 31 2014 07:31:46", "May 31 2014 07:32:24", "May 31 2014 07:32:24",
           "May 31 2014 07:32:26", "May 31 2014 07:32:33", "May 31 2014 07:32:38",
           "May 31 2014 07:32:44", "May 31 2014 07:32:49", "May 31 2014 07:32:52",
           "May 31 2014 07:32:53")

a <- strptime(retest, format="%B %d %Y %H:%M:%S")
a
# [1] NA NA NA NA NA NA NA NA NA NA

我错过了什么?

1 个答案:

答案 0 :(得分:2)

您需要更改区域设置:

# backup original locale
bkp <- Sys.getlocale('LC_TIME')

# change locale
Sys.setlocale('LC_TIME','C')

retest = c("May 31 2014 07:31:46", "May 31 2014 07:32:24", "May 31 2014 07:32:24",
          "May 31 2014 07:32:26", "May 31 2014 07:32:33", "May 31 2014 07:32:38",
          "May 31 2014 07:32:44", "May 31 2014 07:32:49", "May 31 2014 07:32:52",
          "May 31 2014 07:32:53")

a <- strptime(retest, format="%B %d %Y %H:%M:%S")

# restore original locale (if you want...)
Sys.setlocale('LC_TIME',bkp)