【发布时间】:2016-09-26 20:10:40
【问题描述】:
我正在尝试对我的分组因素的所有组合进行 t.test,首先仅根据另一列的选择标准选择和子集数据。
我的数据结构: str(我的数据)
'data.frame': 240 obs. of 6 variables:
$ Group : chr "G1" "G1" "G1" "G1" ...
$ Category: chr "Cat1" "Cat1" "Cat1" "Cat1" ...
$ Subgroup: chr "SG1" "SG1" "SG1" "SG1" ...
$ Score : num 0.156 0.131 0.092 0.319 0.179 ...
$ SD : num 0.0768 0.0768 0.0768 0.0768 0.0768 ...
$ SE : num 0.0172 0.0172 0.0172 0.0172 0.0172 ...
Group 列中有三个组:G1、G2 和 G3
我在Category 列中有四个类别:Cat1、Cat2、Cat3 和 Cat4
我有 12 个子组:SG1、SG2,直到 SG12
目前,我首先通过根据组 ID 对数据进行子集化,为子组名称创建所有组合的列表,因此在本示例中为 G1 和 G3:
combinations <- combn(unique(mydata[mydata$Group %in% c("G1", "G3"),]$Subgroup),2, simplify = FALSE)
然后对这些组合中的每一个进行 t.test,referring to the answer here:
results <- lapply(seq_along(combinations), function (n) {
mydatatemp <- mydata[rownames(mydata$Subgroup) %in% unlist(combinations[n]),]
result <- t.test(mydatatemp[,1], mydatatemp[,2], alternative="two.sided", var.equal=TRUE)
return(result)})
results
我得到的错误如下:
Error in t.test.default(mydatatemp[, 1], mydatatemp[, 2], alternative = "two.sided", :
not enough 'x' observations In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA
有没有更有效的方法来做到这一点?否则,如何纠正这个错误?
更新
其实问题是如何调用t.test公式中Score列的值?
【问题讨论】:
-
@coffeinjunky 不,这些组的大小相同,完全相同。
-
@coffeinjunky 是的,我认为这不是问题所在。问题出在公式上,有问题,我找不到。
-
@coffeinjunky 我用另一个软件试过,t 检验适用于相同的数据。但我想在 R 中做到这一点
-
@coffeinjunky 我不明白你的意思,你能在答案中发表你的评论并提供一个示例代码吗?谢谢
标签: r