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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# all function
  
# Example vector
<x1 <- c(1, 2, 3, - 4, 5)                           
  
all(x1 < 0)  

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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) 
                      

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# any() function
  
# Example vector
x1 <- c(1, 2, 3, - 4, 5, )                           
  
# Apply any function in R
any(x1 < 0)                                            

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)                              

chevron_right


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check this out nerdboinet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.