[R] how to order POSIXt objects ?

Mike Marchywka marchywka at hotmail.com
Mon Feb 14 14:39:19 CET 2011






----------------------------------------
> Date: Mon, 14 Feb 2011 00:55:12 -0800
> From: jon_d_cooke at yahoo.co.uk
> To: r-help at r-project.org
> Subject: [R] how to order POSIXt objects ?
>
>
> 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.


I have been using the approach I stuck with elsewhere ,
convert them to numerics and sort that way. Not sure if
there is a better way but I'm used to long ints of "time since
jan 1 1970 " etc. 

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


More information about the R-help mailing list