Jason Horn
2006-Mar-06 20:45 UTC
[R] Length of a vector of POSIX objects reported incorrectly?
I have a vector of POSIX times/dates (called "times") that I want to plot. But I'm having trouble because R reports the length of the vector incorrectly. Can someone help me figure out what's going on here? This code.... print(times) print(class(times)) print(length(times)) Produces this... . . . [355] "2006-03-06 18:34:00" "2006-03-06 18:32:00" "2006-03-06 18:31:00" [358] "2006-03-06 18:30:00" "2006-03-06 18:29:00" "2006-03-06 18:27:00" [361] "2006-03-06 18:26:00" "2006-03-06 18:25:00" "2006-03-06 18:24:00" [364] "2006-03-06 18:23:00" "2006-03-06 18:21:00" "2006-03-06 18:20:00" [1] "POSIXt" "POSIXlt" [1] 9 Huh? As you can see there are 365 entries in the "times" vector. Why does R think there are only 9? [[alternative HTML version deleted]]
Gabor Grothendieck
2006-Mar-06 21:20 UTC
[R] Length of a vector of POSIX objects reported incorrectly?
POSIXlt stores its data in a 9 component structure so its length is always 9. See R News 4/1 help desk article for more info. To get what you are looking for try using POSIXct, not POSIXlt: length(as.POSIXct(times)) On 3/6/06, Jason Horn <jhorn at bu.edu> wrote:> I have a vector of POSIX times/dates (called "times") that I want to > plot. But I'm having trouble because R reports the length of the > vector incorrectly. Can someone help me figure out what's going on > here? > > This code.... > > print(times) > print(class(times)) > print(length(times)) > > > Produces this... > . > . > . > [355] "2006-03-06 18:34:00" "2006-03-06 18:32:00" "2006-03-06 18:31:00" > [358] "2006-03-06 18:30:00" "2006-03-06 18:29:00" "2006-03-06 18:27:00" > [361] "2006-03-06 18:26:00" "2006-03-06 18:25:00" "2006-03-06 18:24:00" > [364] "2006-03-06 18:23:00" "2006-03-06 18:21:00" "2006-03-06 18:20:00" > [1] "POSIXt" "POSIXlt" > [1] 9 > > Huh? As you can see there are 365 entries in the "times" vector. > Why does R think there are only 9? > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >