我收到一个data.table
我不明白的错误。我想要做的是执行多个t.test
s 作为事后分析。所以这里有一些示例数据:
dt <- data.table(
expand.grid( list( SID = rep( paste0( "x", 1:3), 3 ), MID = paste0( "y", 1:5) ), stringsAsFactors=FALSE ),
A = rnorm(45),
key = c("SID")
) dt
SID MID A
1: x1 y1 -1.4451214
2: x1 y2 -0.6141025
3: x1 y3 -1.0388595
4: x1 y4 -0.8098261
...
这给了我一个奇怪的错误:
dt[ , list( t.test( x=.SD[ J("x1"), A ], y=.SD[ J("x2"), A ] )$p.value ) , by = MID ]
Error in setkey(ans, NULL) :
x may no longer be the character name of the data.table. The possibility was undocumented and has been removed.
我不知道这意味着什么,但所需的输出将类似于
MID p.x1.x2
1: y1 0.1
2: y2 0.2
3: y3 0.3
4: y4 0.4
5: y5 0.5
这就是我最终能够做到的(只是为了给你完整的画面):
combinations <- lapply( as.data.frame( combn( unique(dt$SID), 2 ), stringsAsFactors=FALSE ), identity )
combinations
$V1
[1] "x1" "x2"
$V2
[1] "x1" "x3"
$V3
[1] "x2" "x3"
test.tab <- lapply( combinations, function( .sid, .dt ){
dt[ , list( t.test( x=.SD[ J(.sid[1]), A ], y=.SD[ J(.sid[2]), A ] )$p.value ) , by = MID ]
}, .dt = dt )
test.tab <- as.data.table( as.data.frame( test.tab ) )
任何想法如何避免错误表示赞赏。任何其他获得相同结果的方法也可以。