Skip to content
Prev 306161 / 398506 Next

POSIXct coerced into numeric when filling a data frame

Hi,

You can also try this:
?x1<-list(a=data.frame(x[["a"]]),b=data.frame(x[["b"]]))
library(plyr)
x2<-do.call(rbind.fill,x1)
colnames(x2)<-c("a","b")
x2first<-subset(x2,format(a,"%Y-%m-%d")=="2011-10-30" )
?x2second<-subset(x2,format(b,"%Y-%m-%d")=="2011-10-30" )
y<-data.frame(t(data.frame(a=x2first[,1],b=x2second[,2])))
colnames(y)<-c("first","second")
?y<-data.frame(ID=row.names(y),y)
y<-within(y,{first<-as.POSIXct(first);second<-as.POSIXct(second)})
?y
#? ID?????????????? first????????????? second
#a? a 2011-10-30 15:45:00 2011-10-30 16:00:00
#b? b 2011-10-30 15:30:00 2011-10-30 15:45:00
str(y)
#'data.frame':??? 2 obs. of? 3 variables:
# $ ID??? : Factor w/ 2 levels "a","b": 1 2
# $ first : POSIXct, format: "2011-10-30 15:45:00" "2011-10-30 15:30:00"
?#$ second: POSIXct, format: "2011-10-30 16:00:00" "2011-10-30 15:45:00"
A.K.



----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, September 24, 2012 10:42 AM
Subject: [R] 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:
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"))
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
[1] "2011-10-30 15:45:00 GMT"
+?  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]
+ }
?  ID? ? ? first? ?  second
1? a 1319989500 1319990400
2? b 1319988600 1319989500

where I would need:
? ? ? ? ? ? ? ? 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: