Issue
I am creating an if statement for whether an output contains a specific string or not. I am using regular expression to do so.
I am using the grepl() function to investigate if the output contains the string 'Final evaluation: none (in check)' within the variable 'stockfish_response'. This then contains a logical matrix of FALSEs and TRUEs. E.g:
FALSE FALSE FALSE FALSE TRUE
grepl('Final evaluation: none \\(in check\\)', stockfish_response)
To incorporate this into an IF ELSE statement I need a logical process to check if a TRUE exists at least once in this matrix. Is there a function which will return TRUE if the logical matrix contains at least one TRUE and FALSE if the logical matrix contains no TRUEs.
For instance:
grepl_output <- grepl('Final evaluation: none \\(in check\\)', stockfish_response)
if (grepl_output == TRUE){
print('the phrase \'Final evaluation: none (in check) string \' exists')
} else {
print('the phrase \'Final evaluation: none (in check) string \' does not exist')
}
whereby grepl_output == TRUE is not just constrained to the first logical in the list and returns TRUE if any value in the list is TRUE
Solution
Could you use any?
if any(grepl_output == TRUE) {
# do something ...
}
See ?any for details.
Answered By - ethanknights Answer Checked By - Clifford M. (WPSolving Volunteer)