开发者

Unexpected value for length of a vector in R

I have the character vector below

a = c("2009-07-31 18:00:33", "2009-07开发者_如何学Go-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


The object b has class POSIXlt. Arrays of POSIXlt dates always return a length of 9, since the represent a named list of nine vectors:

R> class(b)
[1] "POSIXt"  "POSIXlt"

R> unclass(b)
$sec
[1] 33 38 43 49 49 16 13 13
$min
[1]  0  0  0  0 58 53  4 16
$hour
[1] 18 18 18 18  1  1  8 16
$mday
[1] 31 31 31 31  1  1  1  1
$mon
[1] 6 6 6 6 7 7 7 7
$year
[1] 109 109 109 109 109 109 109 109
$wday
[1] 5 5 5 5 6 6 6 6
$yday
[1] 211 211 211 211 212 212 212 212
$isdst
[1] 1 1 1 1 1 1 1 1

Class POSIXct, which represents the (signed) number of seconds since the beginning of 1970 as a numeric vector, gives you the expected length:

R> length(as.POSIXct(a))
[1] 8
R> unclass(as.POSIXct(a))
[1] 1.249e+09 1.249e+09 1.249e+09 1.249e+09 1.249e+09 1.249e+09 1.249e+09
[8] 1.249e+09
attr(,"tzone")
[1] ""


As you can see in ?strptime it converts character strings to class POSIXlt. In R there are two types of times: POSIXlt and POSIXct. Description is in ?DateTimeClasses, but to shortcut:

Class "POSIXct" represents the (signed) number of seconds since the beginning of 1970 as a numeric vector.

Class "POSIXlt" is a named list of vectors representing sec 0–61: seconds min 0–59: minutes hour 0–23: hours mday 1–31: day of the month mon 0–11: months after the first of the year. year Years since 1900. wday 0–6 day of the week, starting on Sunday. yday 0–365: day of the year. isdst Daylight savings time flag. Positive if in force, zero if not, negative if unknown.

So your b is list of 9 vectors, 8-length each.

You can see:

sapply(b,length)

You could use exact conversion:

b_1 = as.POSIXlt(a, "%Y-%m-%d %H:%M:%S",tz="")
b_2 = as.POSIXct(a, "%Y-%m-%d %H:%M:%S",tz="")

length(b_1) # 9
length(b_2) # 8


Just for the record, this FAQ issue is about to change in R 2.11.0:

2.11.0 NEW FEATURES

length(POSIXlt) now returns the length of the corresponding abstract timedate-vector rather than always 9 (the length of the underlying list structure). (Wish of PR#14073 and PR#10507.)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜