Use of if and else

1

I have a problem and I can not resolve it. I am creating a function and at some point use if s and else . In the x argument of the function: develop < - function (x, temp, date) I will leave the option for the user to put 5 strings. So I did the if and else in case I put one, the function will do certain thing and so on. My problem is that it's only working on the first string option I put. In the 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 the first works (calb) and for example, the cmeg already of that the object was not found. I already tried to put x == "cmeg" but without success.

Can anyone help me?

    
asked by anonymous 24.05.2018 / 22:25

2 answers

2

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

I'd 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 if/else , I would try to see the value of x with print(x) . Or maybe something like this:

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

This instruction sets the variables in question 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.

    
25.05.2018 / 08:35
3

Your problem is with {} , because I think R is trying to compile everything after the first else at the same time. To make it easier to build the function use ifelse(test, yes, no) . In your case it will look something like this:

ifelse(test, yes, 
 ifelse(test, yes, 
  ifelse(test, yes, 
   ifelse(test, yes, 
    ifelse(test, yes, no)))))
    
25.05.2018 / 02:57