The all & any R Functions | 4 Example Codes

 

In this tutorial, I’ll show you how to use the all and any R functions. I’m going to combine both functions in this article, since the R syntax and the usage of the two functions are basically the same. Let’s start with the basic R syntax.

 

Basic R Syntax:

all(x)
any(x)

 

The all R function checks in a logical vector, if all values are TRUE.
The any R function checks in a logical vector, if any values are TRUE.

In the following tutorial, I’ll illustrate based on four examples how to apply the all and any functions in R.

Let’s get started…

 

Example 1: Basic Application of all() & any()

Let’s begin with a simple example. For the example, I’m going to use the following vector:

x1 <- c(1, 5, 3, - 3, 5, - 7, 8)                           # Example vector
x1                                                         # Print vector to RStudio console
# 1  5  3 -3  5 -7  8

Our example vector is numeric and consists of 7 values.

Now, let’s check if all or any values of this vector are smaller than zero. First, we use the all function:

all(x1 < 0)                                                # Apply all function in R
# FALSE

The R all function returns FALSE, indicating that not all values of our vector are below zero.

Let’s check if any values are below zero:

any(x1 < 0)                                                # Apply any function in R
# TRUE

The R any function returns TRUE, indicating that at least one value of our vector is smaller than zero.

By the way: I have also published a YouTube tutorial on my YouTube channel, in which I’m explaining the basic application of all and any in some more detail. You can find the video here:

 

 

OK, that was the typical application of all and any. However, sometimes there occur problems with NA values (i.e. missing data). How to handle NAs with all and any is what I’m going to show you next…

 

Example 2: The na.rm Option of all & any

For the second example, I’m going to add an NA value to our vector of Example 1:

x2 <- c(x1, NA)                                            # Example vector with NA value
x2                                                         # Print vector to RStudio console
# 1  5  3 -3  5 -7  8 NA

As you can see, our second example vector looks exactly as the first example vector, but this time with an NA value at the last position. Let’s see what happens when we check if all values of our vector are larger than – 10:

all(x2 > - 10)                                             # Apply all function to NA vector
# NA

The R all function returns NA…

What if we check whether any value is less than – 10?

any(x2 < - 10)                                             # Apply any function to NA vector
# NA

NA again…

Dont’t worry! There is an easy fix. We can simply specify the na.rm option as TRUE in order to exclude NA values from our calculation:

all(x2 > - 10, na.rm = TRUE)                               # Apply all function with na.rm = TRUE
# TRUE

After setting na.rm = TRUE, the all command returns TRUE. There are values greater than – 10 in our vector.

Now, let’s do the same exercise with the any R function:

any(x2 < - 10, na.rm = TRUE)                               # Apply any function with na.rm = TRUE
# FALSE

The any command returns FALSE- Our vector contains no value smaller than – 10.

Easy going. Let’s move on to the next example.

 

Example 3: Use all & any in Combination with Other Functions

So far, we have only used the less than and greater than operators as input of all and any. However, in combination with all and any, you can use basically any operator or function that returns a logical vector.

Let’s do an example. With the following R code, I’m producing a logical vector with the is.na function and then I’m using this logical vector as input for all…

all(is.na(x2))                                              # Apply is.na function within all
# FALSE

…and any:

any(is.na(x2))                                              # Apply is.na function within any
# TRUE

As you can see, there are values that are not NA (as indicated by all), but at least one value is NA (as indicated by any).

If you ask me, these functions are very useful. Especially, when you can automatize the process…

 

Example 4: Apply all & any to All Columns of a Data Frame

For this example, I’m going to use the airquality data.frame.

Let’s load the data:

data("airquality")                                          # Load airquality data

And then let’s have a look at the data:

head(airquality)                                            # First 6 rows of airquality data

 

Airquality Data Matrix as Example Data for all and any Functions

Table 1: First Six Rows of Airquality Data.

 

The airquality data set consists of six columns, all of them are the data class numeric. Let’s assume you would like to check in all columns of this data matrix, if all or any values are greater than zero.

With the following code, we can create an empty data matrix first, in which we are going to store our results afterwards:

airq_all_any <- matrix(nrow = 2, ncol = ncol(airquality))   # Create empty matrix for results
colnames(airq_all_any) <- colnames(airquality)              # Colnames of results matrix
rownames(airq_all_any) <- c("all > 0", "any > 0")           # Rownames of results matrix

Now we can use a for-loop, to check all our columns at once:

for(i in 1:ncol(airquality)) {                              # Apply all and any to each column 
  airq_all_any[1, i] <- all(airquality[ , i] > 0)
  airq_all_any[2, i] <- any(airquality[ , i] > 0)
}

Let’s print our results to the R or RStudio console:

airq_all_any                                                # Print results to RStudio console
#         Ozone Solar.R Wind Temp Month  Day
# all > 0    NA      NA TRUE TRUE  TRUE TRUE
# any > 0  TRUE    TRUE TRUE TRUE  TRUE TRUE

As you can see, our results matrix consists of a column for each column of the airquality data as well as of one row indicating if all values are grater that zero and one row indicating if any values are greater than zero.

Of cause you can modify this code for your specific needs, e.g. by specifying na.rm = TRUE (as in Example 2) or by using different ways to produce a logical vector (as in Example 3).

Definitely an awesome way to inspect your data.

 

Video: all, any & Similar R Functions

Videos are sometimes easier to understand. If you need more information on the all and any R functions, I can recommend the following video of the YouTube channel HowTo. The speaker explains all, any as well as other useful R functions in some more detail.

 

 

Further Reading

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top