[R] how to order POSIXt objects ?

JonC jon_d_cooke at yahoo.co.uk
Mon Feb 14 09:55:12 CET 2011


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.



More information about the R-help mailing list