Search code examples
rdata.tablereshape2

dcast (reshape2 package) error with data frame


I have a data.frame which looks like below

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

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

Solution

  • No errors with 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