[R] how to order POSIXt objects ?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Mon Feb 14 18:18:34 CET 2011


a) POSIXt represents the idea of a datetime. POSIXct is a compact 
representation (number of seconds since 1970-01-01 00:00:00 GMT) of this 
idea. POSIXlt is an inefficient but convenient representation (using 
nine separate components) of this idea. Either POSIXct or POSIXlt may be 
treated as a POSIXt, but you cannot create a POSIXt as such... it has to 
be one of the other two types. Strptime returns a POSIXlt, not so 
efficient for sorting and so forth.

b) Because the POSIXct representation is always referenced to GMT, and 
the POSIXlt representation always indicates the timezone and daylight 
savings time, you may find these representations difficult to work with 
for "simple" problems. If your times come from multiple timezones, or 
include the daylight savings time shift (and you need to work with times 
around the autumn changeover) then you have to use POSIXct or POSIXlt. 
Otherwise, you may find that Date or chron classes are easier to work with.

c) Note that a column of data that only includes time should probably be 
converted to "difftime" using as.difftime. Your code is filling in the 
current date on your strptime result, which can get you into trouble 
later if you import other times on other dates and then try to compare 
times you imported previously. There is an as.numeric(z,units="secs") 
that you can use to obtain sortable values on the fly.

tc <- textConnection( "DATE,TIME
18/01/2011,08:00:01
18/01/2011,08:10:01
18/01/2011,08:20:01
18/01/2011,08:30:01
19/01/2011,08:00:01
19/01/2011,08:10:01
19/01/2011,08:20:01
19/01/2011,08:30:01
")
dta <- read.csv( tc, as.is=TRUE )
close( tc )
dta$DATE <- as.Date( dta$DATE, format="%d/%m/%Y" )
dta$TIME <- as.difftime( dta$TIME )
dta.sorted <- dta[order( dta$DATE, -as.numeric( dta$TIME ) ), ]


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



More information about the R-help mailing list