当指定“ by =”时,“ POSIXct”列上的data.table中位数返回“数值”

时间:2019-07-05 09:25:24

标签: r data.table posixct

尽管x[, median(datetime) , by = group]返回“ POSIXct”,但似乎x[, median(datetime) ]时未保留POSIXct类。 调用as.POSIXct返回所需的输出。

我是否有理由明确致电as.POSIXct

下面是使用CRAN上最新的data.table版本的可复制示例。

require(data.table)

x = data.table(v = as.POSIXct(c('2019-07-05 00:00:01', 
'2019-07-05 00:00:02', '2019-07-05 00:00:03', 
'2019-07-05 00:00:04')), g = rep(1:2, each = 2))

o1 = x[, .( V1 = median(v) ) ]

o2 = x[,  .(V1 = median(v) ), by = g]

o3 = x[,  .(V1 = as.POSIXct( median(v) ) ), by = g]

class(o1$V1)
[1] "POSIXct" "POSIXt" 

class(o2$V1)
[1] "numeric"

class(o3$V1)
[1] "POSIXct" "POSIXt" 




我的sessionInfo():

R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 19.04

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.12.2

loaded via a namespace (and not attached):
[1] compiler_3.6.0 tools_3.6.0   

0 个答案:

没有答案