I believe the problem is that when you call t.test(x~GENDER)
, it's evaluating the variable x
within the scope of Dataset
rather than the scope of your function. So it's trying to compare values of x
between the two genders, and is confused because Dataset
doesn't have a variable called x
in it.
A solution that should work is to call:
do.call('t.test', args=list(formula=as.formula(paste0(x,'~GENDER')), data=Dataset))
do.call('t.test', args=list(formula=as.formula(paste0(x,'~GENDER')), var.equal=T, data=Dataset))
which will call t.test()
and pass the value of x
as part of the formula argument rather than the character x (i.e score ~ GENDER
instead of x ~ GENDER
).
The reason for the particular error you saw is that Dataset$GENDER
has length equal to the number of rows in Dataset
, while Dataset$x
has length = 0.