简体   繁体   English

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

[英]Error using t.test - not enough 'x' observations

I have data where methylation has been measured. 我有测量甲基化的数据。

The data: data.frame , 900 observations x 70 patients [30 ctrl, 40 case], all values are numeric , no NA s. 数据: data.frame900观察值x 70患者[30 ctrl,40例],所有值均为numeric ,无NA

I use the following code: 我使用以下代码:

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 

I want pValue to be a vector with one entry for each observation-row. 我希望pValue是一个向量,每个观察行都有一个条目。

EDIT2: Here is an example - shortened but you should get the idea: 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
...

Maybe someone here can help me to figure out what went wrong - maybe I am missing something obvious here. 也许这里有人可以帮助我找出问题所在-也许我在这里遗漏了一些明显的东西。 I have already seen other posts considering this error message but the answers were like 'do you have NAs in your data?' 我已经看过其他有关此错误消息的帖子,但答案就像“您的数据中是否包含NA?”。 'oh yea!' “哦,是的!” - this does not apply for my problem.. Thank you! -这不适用于我的问题。。谢谢!

I'm going to go out on a limb and guess that you want to apply the the t-test for each row in your data.frame and the fields are labeled 'case1','control1', etc. 我会费力地想一下,您想对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)

If you're still having trouble, this bit of code is equivalent and probably easier to debug: 如果您仍然遇到问题,那么这段代码是等效的,并且可能更容易调试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM