unexpected subsetting behavior after gathering with tidyr
What's going on here?
Setup
Create y1
and y2
, equivalent data.frames. y1
is generated using gather
and y2
is read in directly.
library(tidyr)
x <- data.frame(id = 1:2,
day_01 = as.POSIXct(c('2015-03-09', '1989-02-11')),
day_02 = as.POSIXct(c('1985-09-11', '2000-08-15')),
gender = factor(c("M", "F")))
y1 <- gather(x, key, value, day_01, day_02)
y2 <- read.table(text="id gender key value
1 M day_01 2015-03-09
2 F day_01 1989-02-11
1 M day_02 1985-09-11
2 F day_02 2000-08-15", header=TRUE)
y2 <- within(y2, value<-as.POSIXct(as.character(value)))
y1
and y2
are equal
all.equal(y1, y2)
# [1] TRUE
Problem
Subsetting y1
drops the POSIXct column to numeric, but subsetting y2
in the same way does not.
y1[y1$gender == 'M', ]
# id gender key value
# 1 1 M day_01 1.426e+09
# 3 1 M day_02 4.953e+08
y2[y2$gender == 'M', ]
# id gender key value
#1 1 M day_01 2015-03-09
#3 1 M day_02 1985-09-11
Session info
R version 3.1.2 (2014-10-31) Platform: i386-w64-mingw32/i386 (32-bit)
tidyr 0.2.0.9000
链接地址: http://www.djcxy.com/p/77278.html上一篇: 我无法下载R中的reshape2软件包
下一篇: 与tidyr收集后的意外子集行为