Any and All Functions in R Programming

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this lesson, you will learn how R's any and all functions (not literally any and all) are used for implicit iteration of vectors. Working code examples are provided to highlight these functions.

Any and All Functions?!

Don't panic, we aren't actually covering any and all functions in R! We mean the any and the all functions that are a part of R. They are used to check if elements within a vector are True.

These functions use implicit iteration, meaning that they will step through each element of a vector, but you don't have to create a special loop to do so. Think of the seq function: All you have to do is provide a start and an end, and R performs a loop.

The same is true for the any and all functions. These functions are used to search vectors. While you still need to provide a search term, R performs an implicit iteration of the vector, and spits back a TRUE or FALSE. Let's take a look at these functions, starting with any.


The any function in R will tell if you if there are ANY of the given search terms in your vector. It returns either TRUE or FALSE. To demonstrate this function, let's create a quick vector that goes from -3 to 5, incrementing by 1.

y <- seq(-3, 5, by = 1)

Now we can use the any function. There are several ways to use this, the simplest is to enter the function and provide the condition. In our case, we'll check for any negative numbers (e.g., x < 0):

any(x < 0)

Since you only have to provide the name of the vector and the condition, this is the implicit iteration. After entering the previous code and hitting Enter, R will display the following:

[1] TRUE

This means that the vector x contains negative values.

Another option is to create an if statement to check for any negative values in the vector.

if(any(y < 0)) cat("Negative Values Found")

Instead of TRUE, the result will now display the message defined in the if statement.

So far we have determined if any value is negative; next we can check if ALL values meet the condition.


We can run a test to see if all values meet a condition. Since R is a tool for statistics and data science, you may not know what values you have in a given vector.

The following code is a bit advanced, but it creates a distribution; it is displayed here to demonstrate the generation of a sequence in which you don't know the end result.

range(q <- sort(round(stats::rnorm(15) - .6, 1)))

When I ran the previous code, the result was:

[1] -1.0 2.7

Now we can check to see if ALL the numbers are negative. Of course we know the answer, but as part of advanced R data analysis, we may not know. This is where the all function comes in handy: We can use it just as we used the any function.

if(all(q < 0)) cat("All Negative Values Found")

In our case, the result of all(q) is FALSE, and our little message won't display.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account