if Else - Does the position of the keys "{}" make a difference in the result?

Asked

Viewed 47 times

-1

I’m making web Scrapping from a website. And for that I need to check if a data.frame exists or not.

Besides not being working, I realize that I have different results only changing the position of the keys {}

Example 1:

   if(exists(carteiras_i)==T) {
      if (nrow(carteiras)==0){
      carteiras <- carteiras_i
      } else {
      carteiras <- carteiras %>% rbind(carteiras_i); rm(carteiras_i)
      }
   } else (exists(carteiras_i)==F){
      print("Faz nada")
      }


#> Erro: '{' inesperado in:
#> "      }
#>   } else (exists(carteiras_i)==F){"
#> >       print("Faz nada")
#> [1] "Faz nada"
#> >       }
#> Erro: '}' inesperado in "      }"
#> > }
#> Erro: '}' inesperado in "}"

Example 2

   if(exists(carteiras_i)==T) {
      if (nrow(carteiras)==0){
      carteiras <- carteiras_i
      } else {
      carteiras <- carteiras %>% rbind(carteiras_i); rm(carteiras_i)
      }
   } else (exists(carteiras_i)==F){print("Faz nada")}




#> Erro: '{' inesperado in:
#> "      }
#>    } else (exists(carteiras_i)==F){"
#> > }
#> Erro: '}' inesperado in "}"

What am I missing? Why the difference just changing the key?

  • 1

    Try else if (!exists(carteiras_i)) or else if (isFALSE(exists(carteiras_i))). Do not compare a condition or function with a logical value with F/T, the value of the condition or function is already logical.

1 answer

1


You don’t put a condition for Else, I imagine that there at the end you should put a

Else if (exists(portfolios)==F) {}

in place of his

Else (exists(portfolios)==F)

Browser other questions tagged

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