【发布时间】:2014-06-11 00:02:18
【问题描述】:
(这是之前在data-table-help mailing list 上发布的,但是已经有几个星期没有评论了,我做了更多的尝试来调试它。)
我遇到了一个奇怪的错误,互联网搜索只出现在data.table 的提交日志中:
# Error in dcast.data.table(test.table, as.formula(paste(class.col, "+", :
# retFirst must be integer vector the same length as nrow(i)
这是在我通过随机重采样Trial 替换的data.table 上运行先前测试的工作dcast.data.table 表达式时出现的。有问题的部分是这样的:
dcast.data.table(test.table,
Class + Time + Trial ~ Channel,
value.var = "Voltage",
fun.aggregate=identity)
输入表中几乎重复的行似乎令人窒息(即,无论表中是否存在id 列,错误都是相同的):
test.table <- structure(list(Trial = c(1169L, 1169L), Sample = c(155L, 155L
), Class = c(1L, 1L), Subject = structure(c(13L, 13L), .Label = c("s01",
"s02", "s03", "s04", "s05", "s06", "s07", "s08", "s09", "s10",
"s11", "s12", "s13"), class = "factor"), Channel = c(1L, 1L),
Voltage = structure(c(-0.992322316444497, -0.992322316444497
), "`scaled:center`" = -6.23438399446429e-16, "`scaled:scale`" = 1),
Time = c(201.149466192171, 201.149466192171), Baseline = c(0.688151312347969,
0.688151312347969), id = 1:2), .Names = c("Trial", "Sample",
"Class", "Subject", "Channel", "Voltage", "Time", "Baseline",
"id"), class = c("data.table", "data.frame"), row.names = c(NA,
-2L), sorted = "id")
test.table
# Trial Sample Class Subject Channel Voltage Time Baseline id
# 1: 1169 155 1 s13 1 -0.9923223 201.1495 0.6881513 1
# 2: 1169 155 1 s13 1 -0.9923223 201.1495 0.6881513 2
dcast.data.table(test.table,
Class + Time + Trial ~ Channel,
value.var = "Voltage",
fun.aggregate=identity)
# Error in dcast.data.table(test.table, Class + Time + Trial ~ Channel, :
# retFirst must be integer vector the same length as nrow(i)
更改 dcast 公式中的单个列会接近我正在寻找的输出:
test.table[2,Trial:=1170]
dcast.data.table(test.table,
Class + Time + Trial ~ Channel,
value.var = "Voltage",
fun.aggregate=identity)
# Class Time Trial 1
# 1: 1 201.1495 1169 -0.9923223
# 2: 1 201.1495 1170 -0.9923223
data.table 有什么问题?我尝试更改键并弄乱公式术语的顺序只是为了查看,因为我不理解错误,但这不起作用。
如果我用来自reshape2 的常规dcast 替换函数调用,我会得到一个看似无关的错误:
# Error in vapply(indices, fun, .default) : values must be length 0, but FUN(X[[29]]) result is length 1
此时在我的代码中,我不在乎 Trial 的值是否正确,因此我可以通过在公式中将其替换为 id 来解决此问题,但我对更通用的或强大的解决方案。
【问题讨论】:
-
如果您的示例是 reproducible,将会很有帮助。您没有包含示例数据,因此我们可以准确地运行您的代码并遇到相同的错误。
-
我同意,并且我在问题中提到我想知道错误的含义,以便我知道如何以简洁的方式重现它,而不是用大量(可能不相关的)代码。
-
我不会说这些错误是不相关的,两者似乎都表明存在返回值不是它“应该”的长度的情况。将需要一个可重现的示例。尝试找到发生错误的数据的最小子集,然后使用 gist.github 或类似文件来托管数据。
-
我将其缩小到相同行的
dcast并设法得到一个工作示例。
标签: r data.table reshape2 resampling