strptime,as.POSIXct和as.Date返回意外的NA [英] strptime, as.POSIXct and as.Date return unexpected NA

查看:276
本文介绍了strptime,as.POSIXct和as.Date返回意外的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 返回:

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:


详细信息

Details

%a :此
平台上当前语言环境中的缩位工作日名称

%b :此平台当前语言环境中的缩写月份名称<

%b: Abbreviated month name in the current locale on this platform.

请注意,缩写名称是特定于平台的(尽管
标准规定在 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的问题 i对于 as.POSIXct as.POSIXlt as.Date

?as.POSIXct


详细信息

如果格式指定,请记住,一些格式
规范是特定于区域设置的,您可能需要通过<$ c $来适当地设置
LC_TIME 类别c> 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).

。日期


详细信息

适用于可用的
时,使用与字符串相关的区域设置特定转换。这影响了
和月份的名称。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆