我试图用dcast从最新reshape2包(1.2.1)进行非规范化的数据帧(或data.table),其中value.var是POSIXct类型,但所得到的数据帧,该日期值已经失去了他们POSIXct课堂,成为数字。
难道我真的要as.POSIXct()每次生成列,如果我想要的值追溯到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);
library(reshape2);
e <- dcast(d, formula = x ~ y, value.var = "z");
str(e);
上述声明运行的结果(注意:新列c和d是数字时代秒而不是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