使用 t.test 时出错 - 'x' 观察值不足

Error using t.test - not enough 'x' observations

我有测量甲基化的数据。

数据:data.frame900观察x 70患者[30 ctrl,40 case],所有值为numeric,无NAs .

我使用以下代码:

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
...

也许这里有人可以帮助我找出问题所在 - 也许我在这里遗漏了一些明显的东西。我已经看到其他考虑此错误消息的帖子,但答案类似于 'do you have NAs in your data?' 'oh yea!' - 这不适用于我的问题.. 谢谢!

我要大胆猜测你想对 data.frame 中的每一行应用 t 检验,字段标记为 'case1'、'control1', 等等

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)