[R] melt in reshape2 destroying dates?

John Kane jrkrideau at inbox.com
Mon May 13 19:36:02 CEST 2013

When I melt a data frame with some dates I am getting some strange results. I seem to lose the date format with POSIXct and get a row of zeros with POSIXlt

Any suggestions as to what I am messing up? Code and data below.
John Kane
Kingston ON Canada

mydata  <-  structure(list(dd1 = structure(c(1325397600L, 1325401200L, 1325404800L, 
1325408400L, 1325412000L, 1325415600L, 1325419200L, 1325422800L, 
1325426400L, 1325430000L, 1325433600L, 1325437200L, 1325440800L, 
1325444400L, 1325448000L, 1325451600L, 1325455200L, 1325458800L, 
1325462400L, 1325466000L), class = c("POSIXct", "POSIXt"), tzone = ""), 
    dd2 = structure(c(1325426400, 1325412000, 1325426400, 1325422800, 
    1325422800, 1325426400, 1325433600, 1325437200, 1325455200, 
    1325458800, 1325451600, 1325458800, 1325466000, 1325466000, 
    1325476800, 1325476800, 1325480400, 1325466000, 1325480400, 
    1325491200), class = c("POSIXct", "POSIXt"), tzone = ""), 
    emp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("part-time", 
    "self-employed")), period = 1:20), .Names = c("dd1", "dd2", 
"emp", "period"), row.names = c(NA, -20L), class = "data.frame")

  mdat  <-  melt(mydata, id=c("emp","period"))
  # alternatively
  mydata$dd1  <-  as.POSIXlt(mydata$dd1)
  mydata$dd2  <-  as.POSIXlt(mydata$dd2)
  mdat  <-  melt(mydata, id=c("emp","period"))

TRY FREE IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if5
Capture screenshots, upload images, edit and send them to your friends
through IMs, post on Twitter®, Facebook®, MySpace™, LinkedIn® – FAST!

More information about the R-help mailing list