Home > Net >  Declaration with argument length zero in R
Declaration with argument length zero in R

Time:08-11

I have the following statement:

if(Estatu== "INF"){
 MIN <- 5
 } else if (length(Estatu)==0) { 
 MIN <- 0
 }

It's very simple, but Estatu sometimes has INF and other times it's an empty value (character 0).

I have two problems:

  1. When the value is character(0), it indicates the following error:

    Error in if (Estatu== "INF") { : 
      argument has zero length
    

    I would like it not to give me an error because I have already put below that the value can be zero length.

  2. When the declaration is repeated and the value is "0" instead of putting the value 0 in MIN, the data of the declaration that has been executed before remains in MIN. I put in MIN the previous value, the result of the previous execution of this statement.

What I can do?

Thanks for your help

CodePudding user response:

We can use the length expression as the first one so that it gets evaluated before anything else

Estatu <- character(0)
if(length(Estatu) == 0) 0 else if(Estatu == "INF") 5
[1] 0
  • Related