MineSweeper
MineSweeper

Reputation: 584

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

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.

EDIT2: Here is an example - shortened but you should get the idea:

    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?' 'oh yea!' - this does not apply for my problem.. Thank you!

Upvotes: 2

Views: 15031

Answers (1)

Jthorpe
Jthorpe

Reputation: 10204

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.

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)

Upvotes: 4

Related Questions