【问题标题】:Using by with data.table与 data.table 一起使用
【发布时间】:2014-03-20 03:13:00
【问题描述】:

这里我尝试使用 data.table 中的 by 参数对每个组中的预测列进行排名。我一直无法理解为什么以下代码不起作用:

> x.small
       prediction group
 1: -0.0093753015    up
 2:  0.0204832283  down
 3: -0.0091790179  down
 4: -0.0473988803  down
 5:  0.0144955868  down
 6: -0.0139455871  down
 7:  0.0005746896    up
 8: -0.0174406693  down
 9: -0.0180556244  down
10: -0.0343069464    up
> x.small[, rank(prediction), by=group]
Error in rank(prediction) :
  'names' attribute [7] must be the same length as the vector [3]

但是这个示例代码可以正常工作:

> diamonds.dt <- data.table(diamonds[1:10, c('carat', 'color')])
> diamonds.dt
    carat color
 1:  0.23     E
 2:  0.21     E
 3:  0.23     E
 4:  0.29     I
 5:  0.31     J
 6:  0.24     J
 7:  0.24     I
 8:  0.26     H
 9:  0.22     E
10:  0.23     H
> diamonds.dt[, rank(carat), by=color]
    color  V1
 1:     E 3.5
 2:     E 1.0
 3:     E 3.5
 4:     E 2.0
 5:     I 2.0
 6:     I 1.0
 7:     J 2.0
 8:     J 1.0
 9:     H 2.0
10:     H 1.0

任何帮助将不胜感激!

编辑:

好吧,现在我真的不知道发生了什么,这很奇怪。我尝试为@Ananda 制作一个可重现的示例,但无法重新创建错误。我什至尝试在预测列的精确副本上运行排名逻辑,它运行良好:

> x.small[, prediction.copy:=prediction]
> x.small[, rank(prediction.copy), by=group]
    group V1
 1:    up  2
 2:    up  3
 3:    up  1
 4:  down  7
 5:  down  5
 6:  down  1
 7:  down  6
 8:  down  4
 9:  down  3
10:  down  2
> x.small[, rank(prediction), by=group]
Error in rank(prediction) :
  'names' attribute [7] must be the same length as the vector [3]

两个相同的列怎么会有两个不同的结果?

编辑 2:

dput(x.small) 的输出:

> dput(x.small)
structure(list(prediction = structure(c(-0.00937530151309606,
0.0204832283018108, -0.00917901792827827, -0.0473988802836657,
0.0144955868466372, -0.0139455871394683, 0.000574689607249577,
-0.0174406692627376, -0.0180556244204637, -0.0343069463869563
), .Names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"
)), group = c("up", "down", "down", "down", "down", "down", "up",
"down", "down", "up"), prediction.copy = c(-0.00937530151309606,
0.0204832283018108, -0.00917901792827827, -0.0473988802836657,
0.0144955868466372, -0.0139455871394683, 0.000574689607249577,
-0.0174406692627376, -0.0180556244204637, -0.0343069463869563
)), .Names = c("prediction", "group", "prediction.copy"), row.names = c(NA,
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x22f2af8>)

【问题讨论】:

  • 您能否发布一些可重现的代码(或者更确切地说,重现您提到的错误的代码)?如果我复制并粘贴您的代码,它对我来说很好......
  • @Ananda 刚发了更新,这很奇怪
  • 您能否编辑您的问题以包含dput(x.small)
  • 这两列不相同:x.small$prediction 向量是一个命名向量(您可以在dput 输出中看到它,或者在names(x.small$prediction) 中看到它)。为什么你的专栏有名字?删除它们,它可以解决您的问题
  • 即使这对我有用....你使用的是什么版本的“data.table”?

标签: r data.table


【解决方案1】:

我想我会关闭这个。如果您遇到同样的问题,请通过运行 str(x.small) 检查问题列是否为命名向量,并查看向量是否以“命名”一词开头。由于某种原因,在对命名向量进行操作时使用 by 参数会导致问题。这似乎是早期版本的 data.table 中的一个小错误,已在更高版本中修补。要修复它,请升级 data.table 或按照@Frank 的建议使用unname()

x.small[,rank(unname(prediction)), by=group]

【讨论】:

    猜你喜欢
    • 2015-11-01
    • 2021-10-11
    • 2011-10-22
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多