Check if values in a vector are True or not in R Programming – all() and any() Function
all() function in R Language will check in a vector whether all the values are true or not.
Syntax: all(x, na.rm)
Parameters:
x: vector
na.rm: logical, if NA value to removed before result
Example 1:
# R program to illustrate# all function # Example vector<x1 <- c(1, 2, 3, - 4, 5) all(x1 < 0) |
Output:
FALSE
Here in the above code, we have created an example vector and applied all() function on it.
Clearly all the values are not less than zero, one value -4 is less than zero, so the answer is FALSE.
Example 2: Using na.rm argument
# R program to illustrate# all function with na.rm # Example vector with NA valuex2 <- c(1, 2, 3, -4, 5, NA) # Apply all function with na.rm = TRUEall(x2 < - 10, na.rm = TRUE) |
Output:
TRUE
Here in the above code, we set the value of na.rm to TRUE. So the output is TRUE. As all of them are greater than -10 in the above code.
any() Function
any() function will check in vector whether any of the value is true.
Syntax: any(x, na.rm)
Parameters:
x: vector
na.rm: logical, if NA value to removed before result
Example 1: any() function
# R program to illustrate# any() function # Example vectorx1 <- c(1, 2, 3, - 4, 5, ) # Apply any function in Rany(x1 < 0) |
Output:
TRUE
Here in the above code, we have applied any() function. Since one value is “-4” (lesser than 0), so the answer is TRUE.
Example 2: Any function using na.rn argument
# R program to illustrate# any function with na.rm # Example vector with NA valuex2 <- c(1, 2, 3, -4, 5, NA) # Apply any function with na.rm = TRUEany(x2 < - 10, na.rm = TRUE) |
output:
FALSE
Here in the above code, we set the value of na.rm to TRUE. So the output is False. As all of them are lesser than -10 in the above code.


