Skip to content
Prev 306145 / 398506 Next

POSIXct coerced into numeric when filling a data frame

A.K., many thanks for your reply.
I have realised there was a mistake in my code, and that the example 
could have been clearer; my apologies for this, the corrected code is below:

 > x<-list()
 > x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30 
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT", 
"2012-06-22 10:00:00 GMT"))
 > x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30 
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
 >
 >
 > x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
 >
 > y<-data.frame()
 > for (i in 1:length(x)) {
+   y[i,"ID"]<-names(x[i])
+   y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
+   y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
+ }
 > y
   ID      first     second
1  a 1319989500 1319990400
2  b 1319988600 1319989500

where I would need:
 > y
                ID                                   first               
         second
1              a     2011-10-30 15:45:00     2011-10-30 16:00:00
2              b     2011-10-30 15:30:00     2011-10-30 15:45:00

The POSIXct objects in my list do not have the same length; and I need 
to extract those POSIXct values that match a specific date (2011-10-30 
in this example), while keeping them in POSIXct format.

Many thanks
Arnaud
On 24/09/2012 13:29, arun wrote: