Gero
Gero

Reputation: 157

Boolean vector comparison for TRUE/FALSE single results

Imagine I've got a logical vector with the next structure:

logical.vector = c(TRUE, TRUE, FALSE, FALSE)

I want to check if it possess a specific patron, the way I thought was doing:

logical.vector == c(TRUE, FALSE, TRUE, FALSE) 
-------------------------------------------------
[1] TRUE FALSE FALSE TRUE

What is the proper code to practise the test to obtain a single FALSE output

Upvotes: 0

Views: 976

Answers (1)

Speleo4Life
Speleo4Life

Reputation: 134

Similar to all as suggested in the comment by @akrun, one could take the slightly more "backwards" approach of using any, which evaluates to true if at least a one element in a vector is true -- in your example, one element is the same as the standard with respect to its index and value. By evaluating this (input) with !input, meaning NOT input, you can obtain the result you are after.

# Standard
logical.vector = c(TRUE, TRUE, FALSE, TRUE)

# Test Patterns

# Case 1 (non-match): should be FALSE
!any(logical.vector != c(TRUE, FALSE, TRUE, TRUE))
 
# Case 2 (non-match): should be FALSE
!any(logical.vector != c(FALSE, TRUE, FALSE, TRUE)) 

# Case 3 (non-match): should be FALSE
!any(logical.vector != c(TRUE, TRUE, TRUE, TRUE)) 

# Case 4 (non-match): should be FALSE
!any(logical.vector != c(FALSE, FALSE, FALSE, FALSE)) 

# Case 5 (match): should be TRUE
!any(logical.vector != c(TRUE, TRUE, FALSE, TRUE)) 

Upvotes: 1

Related Questions