In this article, you will learn how to use the any()
function in R for implicit vector iteration (not literally). To showcase these functions, working code samples are provided.
What is the any()
function in R?
In the R language, the any()
function may be used to determine whether any values in a vector evaluate to TRUE given some expression. Following the syntax of this function below.
Syntax:
any(..., na.rm = FALSE)
Parameters:
- …: The logical vectors.
- na.rm: If TRUE, the NA values are deleted before computing the result.
How to use the any()
function in R?
Using the any()
function with vectors
Here, we will create three vectors for this example.
# Create vectors vec1 <- c(1, 3, 5, 7, 9) vec2 <- c(2, 4, 6, 8) vec3 <- c(11, 5, 9, 7, 12, 33) # View vectors vec1 vec2 vec3
Output
[1] 1 3 5 7 9
[1] 2 4 6 8
[1] 1 11 5 9 7 12 33
Will we apply any()
function to check vec1 and vec2 in vec3?
# Create vectors vec1 <- c(1, 3, 5, 7, 9) vec2 <- c(2, 4, 6, 8) vec3 <- c(1, 11, 5, 9, 7, 12, 33) # Check any(vec1 %in% vec3) any(vec2 %in% vec3)
Output
[1] TRUE
[1] FALSE
For another example, we will apply any()
function to check the number lower than 5 as follows:
# Create vectors vec1 <- c(1, 3, 5, 7, 9) vec2 <- c(2, 4, 6, 8) vec3 <- c(11, 5, 9, 7, 12, 33) # Check any(vec1 < 5) any(vec2 < 5) any(vec3 < 5)
Output
[1] TRUE
[1] TRUE
[1] FALSE
Using the any()
with a data frame
Similarly, we will create a data frame as follows:
# Create a data frame df <- data.frame( name = c( "Robert A. Jordan", "Lena B. Fontana", "Robert G. Ferguson", "Kathy S. Farley", "William P. Fairbank", "Jeffrey J. Anderson" ), ID = c(11110, 11111, 11112, 11113, 11114, 11115), Math = c(9, 5, 6, 8, 9, 7), English = c(8, 9, 8, 8, 10, 8) ) # View a data frame df
Output
name ID Math English
1 Robert A. Jordan 11110 9 8
2 Lena B. Fontana 11111 5 9
3 Robert G. Ferguson 11112 6 8
4 Kathy S. Farley 11113 8 8
5 William P. Fairbank 11114 9 10
6 Jeffrey J. Anderson 11115 7 8
Now, we will use the any()
function for this data frame. See the code example below; you will get more information.
# Create a data frame df <- data.frame( name = c( "Robert A. Jordan", "Lena B. Fontana", "Robert G. Ferguson", "Kathy S. Farley", "William P. Fairbank", "Jeffrey J. Anderson" ), ID = c(11110, 11111, 11112, 11113, 11114, 11115), Math = c(9, 5, 3, 2, 9, 7), English = c(8, 9, 8, 8, 10, 8) ) # Check if any values are less than 5 in Math and English columns any(df$Math < 5) # TRUE any(df$English < 5) # FALSE # Check if any values are equal to 10 in Math and English columns any(df$Math == 10) # FALSE any(df$English == 10) # TRUE # Check any NA values in Math and English columns any(is.na(df$Math)) # FALSE any(is.na(df$English)) # FALSE
Output
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] FALSE
[1] FALSE
Summary
This article demonstrates how to use the any()
function in the R programming language to check any values in a vector. If you have any questions, please leave a comment below. Thanks for reading!
Maybe you are interested:
- The ln() function in R
- lead function in R: Set the NA values at the end of the R object.
- lowess in R: Scatter Plot Smoothing

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript