mercredi 31 mars 2021

R: Why is the if-statement evaluated twice even if there is no loop?

I know there are already a lot of questions about if-statements, but since they almost all contain loops, I haven't found a suitable answer yet.

I built a dataframe with the most important statistical values (p-values, Cohen's d,...) from 3 tests:

p1 = 0.9
p2 = 0.00021
p3 = 0.001
stat <- data.frame(ID=c("Group A","Group B","Group C"),pvalues = c(p1,p2,p3),cohensd=c(0.14,0.5,0.2))

Now, if at least two p-values are smaller than 0.05, I would like to output a message stating which group has the largest effect (based on Cohen's d) - but of course only if the p-value is smaller than 0.05 for this group.

In my case, the desired output would be "The effect is strongest for Group B."

I tried the following:

if(((p1 < 0.05)+(p2 < 0.05) + (p3 < 0.05))>=2 ){paste("The effect is strongest for", stat$ID[which.max(stat$cohensd)&stat$pvalues<0.05])}

But what I get is: "The effect is strongest for Group B" "The effect is strongest for Group C"

Apparently, the if-statement is evaluated twice, but I can't figure out why. Can someone find my error? Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire