[R] how to order POSIXt objects ?

jim holtman jholtman at gmail.com
Mon Feb 14 18:05:09 CET 2011


Thanks.  I did not even know about that function; will have to use it
in the future.   That is the good news/bad news about R; so many
things to learn about.

On Mon, Feb 14, 2011 at 11:58 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
> See the help page for order.  It has a similar example, and the general
> solution is to use xtfrm, not unclass.
>
> On Mon, 14 Feb 2011, jim holtman wrote:
>
>> 'unclass' it first(assuming that it is POSIXct)
>>
>> -unclass(mytime)
>>
>> On Mon, Feb 14, 2011 at 3:55 AM, JonC <jon_d_cooke at yahoo.co.uk> wrote:
>>>
>>> I have a problem ordering by descending magnitude a POSIXt object. Can
>>> someone help please and let me know how to work around this. My goal is
>>> to
>>> be able to order my data by DATE and then by descending TIME.
>>>
>>> I have tried to include as much info as possible below. The problem stems
>>> from trying to read in times from a CSV file. I have converted the
>>> character
>>> time values to a POSIXt object using the STRPTIME function. I would like
>>> ideally to sort using the order function as below.
>>>
>>> test.sort <- order(test$DATE, -test$mytime)
>>>
>>> However, when I try this I receive the error as below :
>>>
>>> Error in `-.POSIXt`(test2$mytime) :
>>>  unary '-' is not defined for "POSIXt" objects
>>>
>>> To make this easier to understand I have pasted my example data below
>>> with a
>>> list of R commands I have used. Any help or assistance would be
>>> appreciated.
>>>
>>>> test2 <- read.csv("C:/Documents and Settings/Jonathan Cooke/My
>>>> Documents/Downloads/test2.csv", sep=",")
>>>> test2
>>>
>>>        DATE     TIME
>>> 1 18/01/2011 08:00:01
>>> 2 18/01/2011 08:10:01
>>> 3 18/01/2011 08:20:01
>>> 4 18/01/2011 08:30:01
>>> 5 19/01/2011 08:00:01
>>> 6 19/01/2011 08:10:01
>>> 7 19/01/2011 08:20:01
>>> 8 19/01/2011 08:30:01
>>>
>>>> test2$mytime <- strptime(test2$TIME,"%H:%M:%S")
>>>> test2$mytime
>>>
>>> [1] "2011-02-14 08:00:01" "2011-02-14 08:10:01" "2011-02-14 08:20:01"
>>> "2011-02-14 08:30:01" "2011-02-14 08:00:01"
>>> [6] "2011-02-14 08:10:01" "2011-02-14 08:20:01" "2011-02-14 08:30:01"
>>>
>>>> test2
>>>
>>>        DATE     TIME              mytime
>>> 1 18/01/2011 08:00:01 2011-02-14 08:00:01
>>> 2 18/01/2011 08:10:01 2011-02-14 08:10:01
>>> 3 18/01/2011 08:20:01 2011-02-14 08:20:01
>>> 4 18/01/2011 08:30:01 2011-02-14 08:30:01
>>> 5 19/01/2011 08:00:01 2011-02-14 08:00:01
>>> 6 19/01/2011 08:10:01 2011-02-14 08:10:01
>>> 7 19/01/2011 08:20:01 2011-02-14 08:20:01
>>> 8 19/01/2011 08:30:01 2011-02-14 08:30:01
>>>
>>>> test2.sort <- order(test2$DATE, -test2$mytime)
>>>
>>> Error in `-.POSIXt`(test2$mytime) :
>>>  unary '-' is not defined for "POSIXt" objects
>>>
>>> It's at this stage where I have got stuck as I'm new to R and don't yet
>>> know
>>> a way of getting around this error. Thanks in advance.
>>>
>>> JonC
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/how-to-order-POSIXt-objects-tp3304609p3304609.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> Jim Holtman
>> Data Munger Guru
>>
>> What is the problem that you are trying to solve?
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> --
> 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



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list