[R] melt in reshape2 destroying dates?

arun smartpink111 at yahoo.com
Mon May 13 20:22:35 CEST 2013


Hi John,
No problem

If you look at the storage mode:
storage.mode(mydata$dd1)
#[1] "integer"

I think during melt, it 
class(mdat$value)
#[1] "numeric"
 class(mydata$dd1)
#[1] "POSIXct" "POSIXt" 
I guess during melting, this gets coerced to integer class.
You can also do:
 mdat$value<-structure(mdat$value,class=c("POSIXt","POSIXct"))
#or
as.POSIXct(mdat$value,origin="1970-01-01")

class(mdat$value)
#[1] "POSIXt"  "POSIXct"
A.K.


----- Original Message -----
From: John Kane <jrkrideau at inbox.com>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, May 13, 2013 2:11 PM
Subject: Re: [R] melt in reshape2 destroying dates?

Thanks very much.   I could have sworn I had tried that but obviously I had not. 

So melt() is returning the correct value which is what I had suspected but I obiously did not convert it properly.I wonder if this buggy enough to mention the behaviour to the package maintainer?



John Kane
Kingston ON Canada


> -----Original Message-----
> From: smartpink111 at yahoo.com
> Sent: Mon, 13 May 2013 11:05:58 -0700 (PDT)
> To: jrkrideau at inbox.com
> Subject: Re: [R] melt in reshape2 destroying dates?
> 
> In the first case:
>  mdat$value<-.POSIXct(mdat$value,tz="EST")
>  mdat$value[1:3]
> #[1] "2012-01-01 01:00:00 EST" "2012-01-01 02:00:00 EST"
> #[3] "2012-01-01 03:00:00 EST"
>  mydata$dd1[1:3]
> #[1] "2012-01-01 01:00:00 EST" "2012-01-01 02:00:00 EST"
> #[3] "2012-01-01 03:00:00 EST"
> A.K.
> 
> 
> 
> ----- Original Message -----
> From: John Kane <jrkrideau at inbox.com>
> To: r-help at r-project.org
> Cc:
> Sent: Monday, May 13, 2013 1:36 PM
> Subject: [R] melt in reshape2 destroying dates?
> 
> 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.
> Thanks,
> 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")
> 
> library(lubridate)
> library(reshape2)
> 
>   str(mydata)
>   mdat  <-  melt(mydata, id=c("emp","period"))
>   str(mdat)
> 
>   # alternatively
> 
>   mydata$dd1  <-  as.POSIXlt(mydata$dd1)
>   mydata$dd2  <-  as.POSIXlt(mydata$dd2)
>   str(mydata)
>   mdat  <-  melt(mydata, id=c("emp","period"))
>   str(mdat)
> 
> ____________________________________________________________
> 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!
> 
> ______________________________________________
> 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.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!
Check it out at http://www.inbox.com/marineaquarium



More information about the R-help mailing list