dcast from reshape2 turns POSIXct into numeric

342 views
Skip to first unread message

PaulHurleyUK

unread,
Sep 2, 2011, 6:18:04 PM9/2/11
to manipulatr
I'm using reshape2 to melt a datafrmae and then recast it. My
datafrmae contains columns of date time stamps in POSIXct format.
dcast seems to turn those POSIXct values into numeric equivalents. Is
this a bug or am I missing something ?

Example code below

names(airquality) <- tolower(names(airquality))

airquality$datestamp<-as.POSIXct(strftime(paste("2011-",airquality
$month,"-" ,airquality$day, sep="")))

aqm <- melt(airquality, id=c("month", "day", "datestamp"), na.rm=TRUE)

dcast(aqm, day + month ~ variable)

dcast(aqm, datestamp ~ variable)

aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)

dcast(aqm, day + month ~ variable)



> dcast(aqm, day + month ~ variable)

day month ozone solar.r wind temp datestamp

1 1 5 41 190 7.4 67 1304204400

2 1 6 NA 286 8.6 78 1306882800
........

Thanks,

Paul.

Dennis Murphy

unread,
Sep 3, 2011, 2:17:08 AM9/3/11
to PaulHurleyUK, manipulatr
Hi:

On Fri, Sep 2, 2011 at 3:18 PM, PaulHurleyUK <pa...@paulhurley.co.uk> wrote:
> I'm using reshape2 to melt a datafrmae and then recast it.  My
> datafrmae contains columns of date time stamps in POSIXct format.
> dcast seems to turn those POSIXct values into numeric equivalents.  Is
> this a bug or am I missing something ?
>
> Example code below
>
> names(airquality) <- tolower(names(airquality))
>
> airquality$datestamp<-as.POSIXct(strftime(paste("2011-",airquality
> $month,"-" ,airquality$day, sep="")))
>
> aqm <- melt(airquality, id=c("month", "day", "datestamp"), na.rm=TRUE)
>
> dcast(aqm, day + month ~ variable)
>
> dcast(aqm, datestamp ~ variable)

All is fine up to here. Notice that you've kept datestamp as an id
variable so that it retains its class when melted and casted.


>
> aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)

Now datestamp is treated as a(n implicit) measure variable, so its
values get coerced to numeric (along with those of ozone, solar.r,
etc.) during the melt process. value and variable are atomic objects
in the data frame aqm, which means they can only have one class.
Hence, when the values of datestamp are combined with those of the
other four measure variables to form the new, stacked variable
'value', its values get coerced to numeric so that the class of value
returns numeric. Consequently, when you cast this version of aqm,
datestamp is now numeric rather than POSIXct. This is a
well-documented feature of R when atomic objects are combined; here is
a simple, basic example:

# ---------- (digression)
> a <- letters[1:3]
> b <- 4:6
> class(a)
[1] "character"
> class(b)
[1] "integer"
> d <- c(a, b)
> class(d)
[1] "character"
> d
[1] "a" "b" "c" "4" "5" "6"

Since atomic objects can only have one class, the integer values in b,
when combined with the character values in a, are coerced to be of
class character, since integer values can be converted to character
but not vice versa.
#-------------
You can always coerce back into a Date or POSIXct class:

u <- as.Date(aqc$datestamp/86400, origin = '1970-01-01')
> head(u)
[1] "2011-05-01" "2011-06-01" "2011-07-01" "2011-08-01" "2011-09-01"
[6] "2011-05-02"

v <- as.POSIXct(aqc$datestamp, origin = '1970-01-01')
> head(v)
[1] "2011-05-01 08:00:00 PDT" "2011-06-01 08:00:00 PDT"
[3] "2011-07-01 08:00:00 PDT" "2011-08-01 08:00:00 PDT"
[5] "2011-09-01 08:00:00 PDT" "2011-05-02 08:00:00 PDT"

The time zone is local unless you change it with the tz = argument of
as.POSIXct().

HTH,
Dennis


>
> dcast(aqm, day + month ~ variable)
>
>
>
>> dcast(aqm, day + month ~ variable)
>
> day month ozone solar.r wind temp datestamp
>
> 1 1 5 41 190 7.4 67 1304204400
>
> 2 1 6 NA 286 8.6 78 1306882800
> ........
>
> Thanks,
>
> Paul.
>

> --
> You received this message because you are subscribed to the Google Groups "manipulatr" group.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to manipulatr+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/manipulatr?hl=en.
>
>

jim holtman

unread,
Sep 3, 2011, 3:44:02 PM9/3/11
to Dennis Murphy, PaulHurleyUK, manipulatr
Here is another way of converting back to POSIXct; the numeric value
is the number of seconds from the epoch 1/1/1970:

unix2POSIXct <- function (time) structure(time, class = c("POSIXt", "POSIXct"))

--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

Reply all
Reply to author
Forward
0 new messages