I am trying to use the dcast from the last reshape2 (1.2.1) to denormalize a data frame (or data table), where value.var is a POSIXct type, but in the resulting data frame, the date values have lost their POSIXct class and become numeric.
Do I really need, as .POSIXct (), each generated column if I want the values returned as POSIXct, or am I missing something?
x <- c("a","b"); y <- c("c","d"); z <- as.POSIXct(c("2012-01-01 01:01:01","2012-02-02 02:02:02")); d <- data.frame(x, y, z, stringsAsFactors=FALSE); str(d); library(reshape2); e <- dcast(d, formula = x ~ y, value.var = "z"); str(e);
The result of the execution on the operators (note that the new columns c and d represent the numeric seconds of the era instead of POSIXct):
> x <- c("a","b"); > y <- c("c","d"); > z <- as.POSIXct(c("2012-01-01 01:01:01","2012-02-02 02:02:02")); > d <- data.frame(x, y, z, stringsAsFactors=FALSE); > str(d); 'data.frame': 2 obs. of 3 variables: $ x: chr "a" "b" $ y: chr "c" "d" $ z: POSIXct, format: "2012-01-01 01:01:01" "2012-02-02 02:02:02" > library(reshape2); > e <- dcast(d, formula = x ~ y, value.var = "z"); > str(e); 'data.frame': 2 obs. of 3 variables: $ x: chr "a" "b" $ c: num 1.33e+09 NA $ d: num NA 1.33e+09
source share