简体   繁体   中英

Nested if-else with regex in R

I'm working with a list of mathematical expressions . I have identified 6 main patterns of expressions. I'm using a regex to filter out each pattern and write similar expressions to one file and if any expressions doesn't match any of the regex, those will be written to a separate file (Log.txt).

s1 = "(5.0 - 50.0)"

s2 = "((5.0 - 50.0) - 15.0)"

s3 = "(15.0 - (5.0 - 50.0))"

s4 = "((43.0 - 85.0) + (18.0 + 84.0))"

s5 = "(100.0 - ((5.0 - 57.0) + 92.0))"

s6 = "(((12.0 + 89.0) - 73.0) - (58.0 - 90.0))"

I've tried using nested if-else and ifelse() both. The code picks up the first pattern and write it to the "ExpressionType1.txt" then it gives an error. When the pattern is not matching the regex it will not go to the else clause. Below code is only for two types of expressions. Is there any other method to use regex with if-else conditions?

expressionList = read.table("expressionList.txt",header = T,sep = "\n")

for(i in 1:length(expressionList[,1])){

  currentExpression = as.character(expressionList[i,1])  

  ifelse(grepl("^\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",currentExpression, perl = T),
         write(currentExpression,file="ExpressionType1.txt",append=TRUE),
    ifelse(grepl("^\\(\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",currentExpression, perl = T),
                write(currentExpression,file="ExpressionType2.txt",append=TRUE), write(currentExpression,file="Log.txt",append=TRUE)
                        ))


}

nested if-else

  ifelse(grepl("^\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",
          currentExpression, perl = T) == TRUE){

    write(currentExpression,file="ExpressionType1.txt",append=TRUE)

  } else if(grepl("^\\(\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",
          currentExpression, perl = T) == TRUE){ 

    write(currentExpression,file="ExpressionType2.txt",append=TRUE)

  } else{
    write(currentExpression,file="Log.txt",append=TRUE)
  }

Error in ans[test & ok] <- rep(yes, length.out = length(ans))[test & ok] : replacement has length zero

In addition: Warning message:

In rep(yes, length.out = length(ans)) :

'x' is NULL so the result will be NULL

If you play a little bit with some prints here and there(replace them instead of the write functions), then you see that something goes wrong with the write function and negates the ifelse.

A cheap solution whould be something like that.

for(i in 1:length(expressionList[,1])){ # i <- 1 
print(i)  
  currentExpression = as.character(expressionList[i,1])  
 enter1 = FALSE 
 enter2 = FALSE 
  if(grepl("^\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",currentExpression, perl = T)){
         write(currentExpression,file="ExpressionType1.txt",append=TRUE)
    enter1 <- TRUE}
         # print('enter 1'),
         if(grepl("^\\(\\(-?\\d+\\.\\d+\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)\\s[\\+\\-\\*\\/]\\s-?\\d+\\.\\d+\\)$",currentExpression, perl = T)){
                write(currentExpression,file="ExpressionType2.txt",append=TRUE)
           enter2 <- TRUE}

  if(enter1 == F & enter2 == F){ write(currentExpression,file="Log.txt",append=TRUE)}

         # print("enter 2"), print('enter 3')


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM