Use of if and Else

Asked

Viewed 154 times

1

I have a problem, and I can’t fix it. I am creating a function and at a certain point use ifs and Else. In function x argument : develop <- Function(x, temp, date) I will leave the option for the user to put 5 strings. So I did the if and Else If you put one, the function will do a certain thing and so on. My problem is that it is only working in the first string option I put. In others it says that the object was not found.

calb <- "calb"
if (x == calb & temp == 25){
  cat("O valor de p é:", tabela.calb25[[1]]$"Pr(>F)"[1]) 
  x11()
  boxplot(Peso ~ Idade + Substrato , data = dados.calb25) 
} else if (x == calb & temp == 30){ 
  cat("O valor de p é:", tabela.calb30[[1]]$"Pr(>F)"[1]) 
  x11() 
  boxplot(Peso ~ Idade + Substrato , data = dados.calb30) 
} else if (x == calb & temp == 35){
  cat("O valor de p é:", tabela.calb35[[1]]$"Pr(>F)"[1]) 
  x11() 
  boxplot(Peso ~ Idade + Substrato , data = dados.calb35) 
}else if (x == cmeg & temp == 25){ 
  cat("O valor de p é:", tabela.cmeg25[[1]]$"Pr(>F)"[1]) 
  x11()
  boxplot(Peso ~ Idade + Substrato , data = dados.cmeg25)

And so it goes until I finish the 5 options. In this case it works the first (calb) and for example, cmeg already than the object was not found. I already tried to put the x == "cmeg" but to no avail.

Can someone help me ?

  • 1

    This example is not reproducible.

  • @Noobsaibot I was careful, the code does exactly the same as the previous one. The AP problem is in the variable cmeg, not us x11/boxplot. I think it’s better to simplify so we can focus on the problem and not on confusing code. But okay, no problem.

  • @Noobsaibot OK, I’ll answer.

2 answers

3

Your problem is in the use of {}, because I think R is trying to compile everything after the first Else at the same time. To facilitate the construction of the function use ifelse(test, yes, no).
In your case it’ll look something like this:

ifelse(test, yes, 
 ifelse(test, yes, 
  ifelse(test, yes, 
   ifelse(test, yes, 
    ifelse(test, yes, no)))))

2


Firstly, as stated in the comments, the code is not reproducible. For two reasons: 1) we have no data to test solutions; 2) in the question it is said that it is a function but we do not know how it is called.

I would start by simplifying the code using the following:

calb <- "calb"
if (x == calb) {
    if (temp == 25) {
        cat("O valor de p é:", tabela.calb25[[1]]$"Pr(>F)"[1], "\n")
        dados <- dados.calb25
    } else if (temp == 30) {
        cat("O valor de p é:", tabela.calb30[[1]]$"Pr(>F)"[1], "\n")
        dados <- dados.calb30
    } else if (temp == 35) {
        cat("O valor de p é:", tabela.calb35[[1]]$"Pr(>F)"[1], "\n")
        dados <- dados.calb35
    }
} else {
    if (x == cmeg) 
        if (temp == 25) {
            cat("O valor de p é:", tabela.cmeg25[[1]]$"Pr(>F)"[1], "\n")
            dados <- dados.cmeg25
        }
}

x11()
boxplot(Peso ~ Idade + Substrato, data = dados) 

Then, before the if/else, would try to see the value of x with print(x).
Or maybe something like that:

cat("x:", x, "\tcalb:", calb, "\tcmeg:", cmeg, "\n")

This statement prints the variables in question all in a row, separated by tabs ("\t"). And without knowing the results of this, without knowing the values of these variables, it is difficult or even impossible to say more.

  • Thanks for the personal answers. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.