Hazel Berrios
Hazel Berrios

Reputation: 21

Writing if else statement for a T test in R

Can anyone tell me what is wrong with my code? I am not able to get an output, when I try to test it.

Ttest<-function(x,y){
    ifelse(shapiro.test(x)$p > 0.05 & shapiro.test(y)$p > 0.05 & var.test(x,y)$p>0.05, 
        t.test(x,y, var.equal=T, na.rm=T), 
        ifelse(shapiro.test(x)$p > 0.05 & shapiro.test(y)$p > 0.05 & var.test(x,y)$p<=0.05, 
            t.test(x,y, var.equal=F, na.rm=T), wilcoxon.test(x,y, na.rm=T)))
            }

R output:

Ttest(A,B)
logical(0)

Upvotes: 0

Views: 978

Answers (1)

jalapic
jalapic

Reputation: 14202

I'm not sure as to the benefit of putting all of that into a function. Nevertheless, it seems to me that your issue may be with

var.test(x,y)$p>0.05

it should be

var.test(x,y)$p.value>0.05

you can't get the p value by just doing $p with var.test like you can with e.g. shapiro.test

If I do the following...

a<-c(1,3,6,2,4,6,1,4,7,8,4)
b<-c(6,4,7,4,5,7,3,8,6,4,7,8)
Ttest(a,b)

your output is

[[1]] t -1.836405

Upvotes: 3

Related Questions