R 中向量长度的意外值
我有下面的字符向量,
a = c("2009-07-31 18:00:33", "2009-07-31 18:00:38", "2009-07-31 18:00:43", "2009-07-31 18:00:49", "2009-08-01 01:58:49", "2009-08-01 01:53:16", "2009-08-01 08:04:13", "2009-08-01 16:16:13")
我想将其转换为时间对象,所以我这样做:
b = strptime(a, "%Y-%m-%d %H:%M:%S")
为什么 a 和 b 有不同的长度?
> length(a)
[1] 8
> length(b)
[1] 9
I have the character vector below
a = c("2009-07-31 18:00:33", "2009-07-31 18:00:38", "2009-07-31 18:00:43", "2009-07-31 18:00:49", "2009-08-01 01:58:49", "2009-08-01 01:53:16", "2009-08-01 08:04:13", "2009-08-01 16:16:13")
I want to convert this to time objects so I do this:
b = strptime(a, "%Y-%m-%d %H:%M:%S")
Why do a and b have different lengths?
> length(a)
[1] 8
> length(b)
[1] 9
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对象
b
具有类POSIXlt
。POSIXlt
日期数组始终返回长度 9,因为 表示九个向量的命名列表:
类 POSIXct,它表示自开始以来的(有符号)秒数
1970 作为数字向量,给出预期的长度:
The object
b
has classPOSIXlt
. Arrays ofPOSIXlt
dates always return a length of 9,since the represent a named list of nine vectors:
Class
POSIXct
, which represents the (signed) number of seconds since the beginning of1970 as a numeric vector, gives you the expected length:
正如您在
?strptime
中看到的,它将字符串转换为POSIXlt
类。 R 中有两种类型的时间:POSIXlt
和POSIXct
。说明在
?DateTimeClasses
中,但要快捷方式:所以你的
b
是 9 个向量的列表,每个向量有 8 个长度。您可以看到:
您可以使用精确转换:
As you can see in
?strptime
it converts character strings to classPOSIXlt
. In R there are two types of times:POSIXlt
andPOSIXct
.Description is in
?DateTimeClasses
, but to shortcut:So your
b
is list of 9 vectors, 8-length each.You can see:
You could use exact conversion:
仅供记录,此常见问题解答问题即将在 R 2.11.0 中更改:
这是 12 月 2 日 RSS 提要条目中的内容,总结了 Subversion 存档(开发人员页面)中的日常更改 有关提要的详细信息。
Just for the record, this FAQ issue is about to change in R 2.11.0:
That's from the December 2 entry of the RSS feed summarising daily changes in the Subversion archive, the developer page for details about the feed.