You get an error because , you get a for at least one sub-group , unique status value for all score's.
This reproduce the error, the status is unique (equal to 1) for all scores.
dx = read.table(text=' score status
1 1 1
2 2 1
3 3 1 ')
t.test(score ~ status, data = dx)
Error in t.test.formula(score ~ status, data = dx) :
grouping factor must have exactly 2 levels
this correct the problem but create another known problem with t.test
, you should have enough observations( I think >= 2):
dx = read.table(text=' score status
1 1 1
2 2 1
3 3 2 ')
t.test(score ~ status, data = dx)
Error in t.test.default(x = 1:2, y = 3L) : not enough 'y' observations
Finally this correct all the problems:
dx = read.table(text=' score status
1 1 1
2 2 1
3 3 2
4 4 2')
t.test(score ~ status, data = dx)
Welch Two Sample t-test
data: score by status
t = -2.8284, df = 2, p-value = 0.1056
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-5.042435 1.042435
sample estimates:
mean in group 1 mean in group 2
1.5 3.5
EDIT I explain the problem without giving a solution, because you don't give a reproducible example.
one solution is do computation only for good groups:
ddply(df, c("freq","snpsincluded"), function(x)
{
if(length(unique(x$status)==2)
pval=t.test(score~status,data=x)$p.value
})