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
<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
x2 <- c (1, 2, 3, -4, 5, NA )
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
x1 <- c (1, 2, 3, - 4, 5, )
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
x2 <- c (1, 2, 3, -4, 5, NA )
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Dec, 2021
Like Article
Save Article