Open In App

Check if values in a vector are True or not in R Programming – all() and any() Function

Last Updated : 23 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to check if the values in a vector are true or not in R Programming Language.

R – all() 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: Basic example of all() function in r

R




# 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() functions to 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




# R program to illustrate
# all function with na.rm
 
# Example vector with NA value
x2 <- c(1, 2, 3, -4, 5, NA)
 
# Apply all function with na.rm = TRUE
all(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.

 R – any() function

any() function in R Programming language will check in vector whether any of the values 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




# R program to illustrate
# any() function
 
# Example vector
x1 <- c(1, 2, 3, - 4, 5, )                       
 
# Apply any function in R
any(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




# R program to illustrate
# any function with na.rm
 
# Example vector with NA value
x2 <- c(1, 2, 3, -4, 5, NA)
 
# Apply any function with na.rm = TRUE
any(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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads