Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Declaration with argument length zero in R

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading