使用t.test时出错 - 没有足够的'x'观察

时间:2015-02-07 15:32:25

标签: r p-value

我有测量甲基化的数据。

数据:data.frame900观察x 70患者[30 ctrl,40例],所有值均为numeric,无NA s。

我使用以下代码:

group <- function(dFrame,toMatch)
{
  matches <- unique (grep(paste(toMatch,collapse="|"), 
                      colnames(dFrame), value=TRUE))
  return(dFrame[matches])
}

pValue <- sapply(methySample, function(x) t.test( group (x,'case'),group (x,'ctrl'))$p.value)


Error in t.test.default(group (x, "case"), group (x, "ctrl")) : 
  not enough 'x' observations 

我希望pValue是一个向量,每个观察行有一个条目。

EDIT2:这是一个例子 - 缩短但你应该明白:

    case_01     case_02     case_03     ctrl_01     ctrl_02    ...
1   0.876729    0.8760000   0.8835130   0.8999369   0.8642505
2   0.8270763   0.7983686   0.8092107   0.8610273   0.8475543
3   0.2591350   0.2829770   0.2735919   0.2556579   0.2735417
4   0.8181337   0.8007408   0.7808821   0.8097073   0.7511147
5   0.6217151   0.6061754   0.5850365   0.6151368   0.5680856
6   0.6943685   0.7605200   0.6855676   0.6687362   0.7320926
...

也许有人可以帮我弄清楚出了什么问题 - 也许我错过了一些明显的东西。我已经看过考虑此错误消息的其他帖子,但答案就像是“您的数据中是否有NA?” “哦,是的!” - 这不适用于我的问题.. 谢谢!

1 个答案:

答案 0 :(得分:4)

我想走出困境,猜测你想对data.frame中的每一行应用t检验,并且字段标记为&#39; case1&#39;,& #39; control1&#39;等

methySample  <-  
    data.frame(case1=rnorm(10),
               case2=rnorm(10),
               control1=rnorm(10),
               control2=rnorm(10))

# identify the fields that are labeled 'case' and 'control'
caseFields <- grep('case',colnames(methySample), value=TRUE)
controlFields <- grep('control',colnames(methySample), value=TRUE)

# apply the t-test for each row (margin = 1)
apply(methySample,
      1,
      function(x)
          t.test(x[caseFields],
                 x[controlFields])$p.value)

如果您仍然遇到问题,这段代码是等效的,可能更容易调试:

pValue <- numeric(0)
for(i in seq(nrow(methySample)))
    pValue  <-  c(pValue,
                  t.test(methySample[i,caseFields],
                         methySample[i,controlFields])$p.value)
相关问题