strptime、as.POSIXct 和 as.Date 返回意外的 NA [英] strptime, as.POSIXct and as.Date return unexpected NA
问题描述
当我尝试解析以下格式的时间戳时:Thu Nov 8 15:41:45 2012",仅返回 NA
.
When I try to parse a timestamp in the following format: "Thu Nov 8 15:41:45 2012", only NA
is returned.
我使用的是 Mac OS X、R 2.15.2 和 Rstudio 0.97.237.我的操作系统的语言是荷兰语:我想这与它有关.
I am using Mac OS X, R 2.15.2 and Rstudio 0.97.237. The language of my OS is Dutch: I presume this has something to do with it.
当我尝试 strptime
时,返回 NA
:
When I try strptime
, NA
is returned:
var <- "Thu Nov 8 15:41:45 2012"
strptime(var, "%a %b %d %H:%M:%S %Y")
# [1] NA
as.POSIXct
也不起作用:
as.POSIXct(var, "%a %b %d %H:%M:%S %Y")
# [1] NA
我也在上面的字符串上尝试了 as.Date
但没有 %H:%M:%S
组件:
I also tried as.Date
on the string above but without %H:%M:%S
components:
as.Date("Thu Nov 8 2012", "%a %b %d %Y")
# [1] NA
知道我可能做错了什么吗?
Any ideas what I could be doing wrong?
推荐答案
我认为正如您所猜测的那样,strptime
由于您的语言环境而无法解析您的日期时间字符串.您的字符串包含缩写的工作日 (%a
) 和缩写的月份名称 (%b
).?strptime
中描述了这些时间规范:
I think it is exactly as you guessed, strptime
fails to parse your date-time string because of your locales. Your string contains both abbreviated weekday (%a
) and abbreviated month name (%b
). These time specifications are described in ?strptime
:
详情
%a
:在当前语言环境中的缩写工作日名称 平台
%a
: Abbreviated weekday name in the current locale on this
platform
%b
:本平台当前语言环境中的月份缩写.
"请注意,缩写名称是特定于平台的(尽管标准指定在 C
语言环境中,它们必须是前三个英文名称的大写字母:"
"Note that abbreviated names are platform-specific (although the
standards specify that in the C
locale they must be the first three
letters of the capitalized English name:"
如果您想使用,了解缩写是必不可少的%a
、%b
或 %h
作为输入格式的一部分:参见示例如何检查."
"Knowing what the abbreviations are is essential if you wish to use
%a
, %b
or %h
as part of an input format: see the examples for
how to check."
另见
[...] locales
查询或设置区域设置.
[...] locales
to query or set a locale.
locales
的问题也与 as.POSIXct
、as.POSIXlt
和 as.Date
相关.
The issue of locales
is relevant also for as.POSIXct
, as.POSIXlt
and as.Date
.
来自?as.POSIXct
:
详情
如果指定了format
,记住一些格式规范是特定于区域设置的,您可能需要设置LC_TIME
通过 Sys.setlocale
适当地分类.这个最常影响%b
、%B
(月份名称)和%p
(AM/PM)的使用.
If format
is specified, remember that some of the format
specifications are locale-specific, and you may need to set the
LC_TIME
category appropriately via Sys.setlocale
. This most often
affects the use of %b
, %B
(month names) and %p
(AM/PM).
从 ?as.Date
:
详情
使用特定于语言环境的字符串之间的转换在适当和可用的情况下.这会影响日期的名称和几个月.
Locale-specific conversions to and from character strings are used where appropriate and available. This affects the names of the days and months.
<小时>
因此,如果字符串中的工作日和月份名称与当前语言环境中的不同,strptime
、as.POSIXct
和 as.Date
无法正确解析字符串并返回 NA
.
Thus, if weekdays and month names in the string differ from those in the current locale, strptime
, as.POSIXct
and as.Date
fail to parse the string correctly and NA
is returned.
但是,您可以通过更改locales
来解决此问题:
However, you may solve this issue by changing the locales
:
# First save your current locale
loc <- Sys.getlocale("LC_TIME")
# Set correct locale for the strings to be parsed
# (in this particular case: English)
# so that weekdays (e.g "Thu") and abbreviated month (e.g "Nov") are recognized
Sys.setlocale("LC_TIME", "en_GB.UTF-8")
# or
Sys.setlocale("LC_TIME", "C")
#Then proceed as you intended
x <- "Thu Nov 8 15:41:45 2012"
strptime(x, "%a %b %d %H:%M:%S %Y")
# [1] "2012-11-08 15:41:45"
# Then set back to your old locale
Sys.setlocale("LC_TIME", loc)
使用我的个人语言环境,我可以重现您的错误:
With my personal locale I can reproduce your error:
Sys.setlocale("LC_TIME", loc)
# [1] "fr_FR.UTF-8"
strptime(var,"%a %b %d %H:%M:%S %Y")
# [1] NA
这篇关于strptime、as.POSIXct 和 as.Date 返回意外的 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!