简体   繁体   English

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

[英]dcast (reshape2 package) error with data frame

I have a data.frame which looks like below 我有一个如下所示的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")

Now I wanted to dcast this data.frame using reshape package as follows 现在我想使用如下的reshape包来抛弃这个data.frame

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

But with that I am getting below error - * 但是与此同时我正在错误以下-*

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

* *

Any pointer why is error is coming, would be helpful. 任何指针为什么会出现错误,都会有所帮助。

> 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

It's to do with NA being logical rather than numeric; 这与NA是逻辑而非数字有关; change to NA_real_ to resolve the error: 更改为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

No errors with data.table 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

I believe the problem might be that you have "c" in your b column, as a value, and "c" as a name column, as our value.var 我相信问题可能是您在b列中有“ c”作为值,在名称列中有“ c”作为我们的value.var

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM