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:
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?
CodePudding user response:
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)