[R] question about sorting POSIXt vector

Gabor Grothendieck ggrothendieck at myway.com
Thu Feb 10 02:57:43 CET 2005


bogdan romocea <br44114 <at> yahoo.com> writes:

: 
: Dear useRs,
: 
: How come the first attempt to sort a POSIXt vector fails (Error:
: non-atomic type in greater), while the second succeeds? (Code inserted
: below.) The documentation says that POSIXt is used to allow operations
: such as subtraction, so I'd expect sorting to work. Is this perhaps an
: OS issue? (I run R 2.0.1 on Win xp.)
: 
: Thank you,
: b.
: 
: #------------code
: test <- c("2005-02-08 18:49:15","2005-02-07 18:36:54",
: 	"2005-02-04 18:37:03","2005-02-06 18:29:04")
: test <- strptime(test,format="%Y-%m-%d %H:%M:%S")
: order(test,decreasing=F)	#doesn't work - why?
: tst <- test + 0
: order(tst,decreasing=F)	#works - how come?
: print(tst)
: #------------run
: > test <- c("2005-02-08 18:49:15","2005-02-07 18:36:54",
: + "2005-02-04 18:37:03","2005-02-06 18:29:04")
: > test <- strptime(test,format="%Y-%m-%d %H:%M:%S")
: > order(test,decreasing=F)#doesn't work - why?
: Error in order(test, decreasing = F) : non-atomic type in greater
: > tst <- test + 0
: > order(tst,decreasing=F)#works - how come?
: [1] 3 4 2 1
: > print(tst)
: [1] "2005-02-08 18:49:15 Eastern Standard Time" "2005-02-07 18:36:54
: Eastern Standard Time"
: [3] "2005-02-04 18:37:03 Eastern Standard Time" "2005-02-06 18:29:04
: Eastern Standard Time"
: >

Because test is was produced by strptime which returns a POSIXlt value
(which is stored as a list of values) while tst is set by an addition
which returns a POSIXct value (i.e. stored as numbers) and you can sort the
numbers but not the lists.

R> class(tst)
[1] "POSIXt"  "POSIXct"
R> unclass(tst)
[1] 1107906555 1107819414 1107560223 1107732544
attr(,"tzone")
[1] ""


R> class(test)
[1] "POSIXt"  "POSIXlt"
R> unclass(test)
$sec
[1] 15 54  3  4

$min
[1] 49 36 37 29

$hour
[1] 18 18 18 18

$mday
[1] 8 7 4 6

$mon
[1] 1 1 1 1

$year
[1] 105 105 105 105

$wday
[1] 2 1 5 0

$yday
[1] 38 37 34 36

$isdst
[1] 0 0 0 0




More information about the R-help mailing list