how do you do the equivalent of Excel's AND() and OR() operations in R?

Viewed 38

drives_DF$block_device == ""
[1] TRUE TRUE TRUE FALSE TRUE

How do I reduce this down to a single FALSE like doing an AND() in Excel?

How do I reduce this down to a single TRUE like doing an OR() in Excel?

2 Answers

Wrapping your code with all() will return TRUE if all evaluated elements are TRUE

all(drives_DF$block_device == "")
[1] FALSE

Wrapping your code with any() will return TRUE if at least one of the evaluated elements is TRUE

any(drives_DF$block_device == "")
[1] TRUE

You can use any and all functions available in R to get the required like this:

#Considering a vector of boolean values
boolVector =  c(F,T,F,T,F)
print(all(boolVector, na.rm = FALSE)) #AND OPERATION
print(any(boolVector, na.rm = FALSE)) #OR OPERATION

The output of the print statements are:

[1] FALSE
[1] TRUE
Related