簡體   English   中英

數據幀的dcast(reshape2程序包)錯誤

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

我有一個如下所示的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

這與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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM