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

Is the operator OR sensitive to the position of the argument that follows it

I want to write fairly concise, more readable R code.
I try to go to the line each time to avoid having very long codes. I have noticed that I have different results depending on whether I go to the line or not after the OR operator in grepl. And that annoys me

For example with this code. I have:

sigaps$Oncologie<-ifelse(
  
  grepl("Radioth[ée]rapie|Chimioth[ée]rapie|Radiochimioth[ée]rapie|Cancer|Tumeur|Tumoral",
        sigaps$Titre.de.l.étude,
        ignore.case=TRUE),1,0)
table(sigaps$Oncologie)

  0   1
377 157

But when I moved Tumoral to the next line, I have a different result. I dont understand what doesn’t
works:

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

sigaps$Oncologie<-ifelse(

  grepl("Radioth[ée]rapie|Chimioth[ée]rapie|Radiochimioth[ée]rapie|Cancer|Tumeur|
         Tumoral",
        sigaps$Titre.de.l.étude,
        ignore.case=TRUE),1,0)
table(sigaps$Oncologie)

  0   1
380 154

I have always done this. But I’m wondering, if I can’t get the same results with two different ways of coding that I find identical, am I not making a coding mistake for years?

>Solution :

The difference occurs because your are breaking the line and adding spaces into your string by splitting it on to the next line and indenting it. Fix it by a) not doing that or b) creating your string with paste(..., sep="|")

grepl(paste("Radioth[ée]rapie", "Chimioth[ée]rapie",
            "Radiochimioth[ée]rapie", "Cancer", 
            "Tumeur", "Tumoral", sep="|"),
      sigaps$Titre.de.l.étude, ignore.case=TRUE)
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