mardi 10 juillet 2018

R - multiple actions after the if statement

My question is how to put in multiple actions after the if statement. For example:

vartest <- var.test(var1 ~ group, alternative='two.sided', conf.level=.95, data=data1)
ttest <- t.test(var1~group, alternative='two.sided', conf.level=.95, var.equal=FALSE, data=data1)
if (vartest$p.value>0.05) {
  if (ttest$p.value<=0.05) {
    cat(ttest$p.value)
    ggboxplot(data1, x="group", y="var1", color="group", palette=c("#00AFBB", "#E7B800"), ylab="var1", xlab="group")
    group_by(data1, group) %>% summarise(count = n(), mean = mean(var1, na.rm = TRUE), sd = sd(var1, na.rm = TRUE))
  } else{
    cat("text1.")
    }
} else{
  cat("text2.")
  }

It isn't giving me my desired result, instead only the first and the last part of the expressions was printed to the console. If I change the order of the actions for that:

vartest <- var.test(var1 ~ group, alternative='two.sided', conf.level=.95, data=data1)
ttest <- t.test(var1~group, alternative='two.sided', conf.level=.95, var.equal=FALSE, data=data1)
if (vartest$p.value>0.05) {
  if (ttest$p.value<=0.05) {
    cat(ttest$p.value)
    group_by(data1, group) %>% summarise(count = n(), mean = mean(var1, na.rm = TRUE), sd = sd(var1, na.rm = TRUE))
    ggboxplot(data1, x="group", y="var1", color="group", palette=c("#00AFBB", "#E7B800"), ylab="var1", xlab="group")
  } else{
    cat("text1.")
    }
} else{
  cat("text2.")
  }

Only the first and the last part of the expressions are printed to the console.

Any ideas how can I run all actions between if and else? Please help!

Aucun commentaire:

Enregistrer un commentaire