与tidyr收集后的意外子集行为

这里发生了什么?

建立

创建y1y2 ,等价的data.frames。 使用gather生成y1并直接读入y2

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)))

y1y2是相等的

all.equal(y1, y2)
# [1] TRUE

问题

子集y1将POSIXct列删除为数字,但以相同的方式对y2进行子集。

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

会话信息

R版本3.1.2(2014-10-31)平台:i386-w64-mingw32 / i386(32位)

tidyr 0.2.0.9000

链接地址: http://www.djcxy.com/p/77277.html

上一篇: unexpected subsetting behavior after gathering with tidyr

下一篇: colSds does not work since the last matrixstats update in R