用data.table进行矩阵操作,规范不正确?

问题描述 投票:1回答:1

我想用 dcast.data.tableas.matrix的方式来使用它。dcast但我却无法达到可比的效果。我将感谢任何有关这方面的建议。

library(reshape2)
library(data.table)
data(ChickWeight)

# this returns the correct dimension:
> as.matrix(dcast(ChickWeight, weight ~ Diet, value.var = "Time")[,-1])
Aggregation function missing: defaulting to length
        1 2 3 4
  [1,]  1 0 0 0
  [2,]  2 3 2 1
  [3,]  1 2 0 2
      .....

# this doesn't
    > as.matrix(dcast.data.table(setDT(ChickWeight), weight ~ Diet, value.var = "Time")[,-1])
Aggregate function missing, defaulting to 'length'
     [,1]
[1,]   -1
r data.table
1个回答
0
投票

在当前CRAN版本的 data.table,下面就可以了。

as.matrix(dcast.data.table(setDT(ChickWeight), weight ~ Diet, 
                      value.var = "Time")[,-1])
© www.soinside.com 2019 - 2024. All rights reserved.