[R] question about sorting POSIXt vector

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Feb 10 08:55:06 CET 2005


You can order (which is not the same as sort) POSIXct vectors but not 
POSIXlt ones.

class() on your objects (or str(), but that only shows one class) would 
have been revealing.

It is somewhat fortuitous that you can order and sort POSIXct vectors, as 
you are ordering the underlying numeric representation.

On Wed, 9 Feb 2005, bogdan romocea wrote:

> 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.

I didn't know sorting was like subtraction. What I found was

      Logical comparisons and limited arithmetic are available for both
      classes.

Try the documentation on order() and sort(), which is clear that these 
apply only to certain sorts of vectors, and for DateTimeClasses which 
tells you POSIXlt is a list and POSIXct is a numeric vector.


> 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"
>>
>
> ______________________________________________
> 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
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list