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">
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
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
Hi Bogdan the behaviour is result of ct and lt POSIX class try> str(test)`POSIXlt', format: chr [1:4] "2005-02-08 18:49:15" "2005-02-07 18:36:54" "2005-02-04 18:37:03" "2005-02-06 18:29:04"> str(tst)`POSIXct', format: chr [1:4] "2005-02-08 18:49:15" "2005-02-07 18:36:54" "2005-02-04 18:37:03" "2005-02-06 18:29:04">or> length(tst)[1] 4> length(test)[1] 9>and read help pages about POSIX how to convert Cheers Petr On 9 Feb 2005 at 17:38, 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. 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.htmlPetr Pikal petr.pikal at precheza.cz