数据帧的dcast(reshape2程序包)错误

时间:2019-07-26 12:04:56

标签: r data.table reshape2

我有一个如下所示的data.frame

sss = structure(list(a = structure(c(16266, 16267, 16268, 16269, 16271,
16272, 16273, 16274, 16275, 16276), class = "Date"), b = c("xxx",
"xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "c", "xxx"),
    c = c(1.35668, 1.352385, 1.35258, 1.35245, 1.352415, 1.35214,
    1.346835, 1.346215, 1.34635, 1.343145)), row.names = c(NA,
-10L), class = "data.frame")

现在我想使用如下的reshape包来抛弃此data.frame

reshape2::dcast(sss, formula = a ~ b, fun.aggregate = mean, fill = NA, value.var = "c")

但是,我的错误却越来越少了- *

Error in vapply(indices, fun, .default) : values must be type 'logical',
 but FUN(X[[9]]) result is type 'double'

*

任何指针为什么会出现错误,都会有所帮助。

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.2 LTS

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

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

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

loaded via a namespace (and not attached):
[1] compiler_3.6.1 magrittr_1.5   plyr_1.8.4     tools_3.6.1    reshape2_1.4.3
[6] Rcpp_1.0.1     stringi_1.4.3  stringr_1.4.0

2 个答案:

答案 0 :(得分:2)

NA是逻辑而不是数字有关;更改为NA_real_以解决错误:

reshape2::dcast(sss, formula = a ~ b, fun.aggregate = mean, fill = NA_real_, value.var = "c")

            a       c      xxx
1  2014-07-15      NA 1.356680
2  2014-07-16      NA 1.352385
3  2014-07-17      NA 1.352580
4  2014-07-18      NA 1.352450
5  2014-07-20      NA 1.352415
6  2014-07-21      NA 1.352140
7  2014-07-22      NA 1.346835
8  2014-07-23      NA 1.346215
9  2014-07-24 1.34635       NA
10 2014-07-25      NA 1.343145

答案 1 :(得分:0)

data.table

没有错误
library(data.table)    
dcast.data.table(setDT(sss), a~ b, fun.aggregate = mean, fill = NA, value.var = 'c')
                 a       c      xxx
     1: 2014-07-15      NA 1.356680
     2: 2014-07-16      NA 1.352385
     3: 2014-07-17      NA 1.352580
     4: 2014-07-18      NA 1.352450
     5: 2014-07-20      NA 1.352415
     6: 2014-07-21      NA 1.352140
     7: 2014-07-22      NA 1.346835
     8: 2014-07-23      NA 1.346215
     9: 2014-07-24 1.34635       NA
    10: 2014-07-25      NA 1.343145

我认为问题可能在于您在b列中有“ c”作为值,而在名称列中有“ c”作为我们的value.var