How to Use all() and any() Functions in R (With Examples)


The all() and any() functions in R can be used to check if all or any values in a vector evaluate to TRUE for some expression.

These functions use the following syntax:

#check if all values in x are less than 10
all(x < 10)

#check if any values in x are less than 10
any(x < 10)

The following examples show how to use each function in practice.

Example 1: Use all() and any() with Vector

We can use the following all() and any() functions to check if all or any values in a vector are less than 10:

#define vector of data values
data <- c(3, 4, 4, 8, 12, 15)

#check if all values are less than 10
all(data < 10)

[1] FALSE

#check if any values are less than 10
any(data < 10)

[1] TRUE

The all() function evaluates to FALSE because not all values in the vector are less than 10.

The any() function evaluates to TRUE because at least one value in the vector is less than 10.

Example 2: Use all() with NA Values

If we use the all() function with a vector that has NA values, we may receive NA as a result: 

#define vector of data values with some NA values
data <- c(3, 4, 4, 8, NA, NA)

#check if all values are less than 10
all(data < 10)

[1] NA

To avoid this, we must specify na.rm=TRUE to first remove the NA values from the vector before checking if all values meet some condition:

#define vector of data values with some NA values
data <- c(3, 4, 4, 8, NA, NA)

#check if all values are less than 10 (and ignore NA values)
all(data < 10, na.rm=TRUE)

[1] TRUE

The all() function now evaluates to TRUE because every value in the vector is less than 10, assuming we ignore NA values.

Example 3: Use all() and any() with Data Frame Columns

We can also use the all() and any() functions to evaluate expressions for data frame columns.

For example, suppose we have the following data frame in R:

#define data frame
df <- data.frame(points=c(30, 22, 19, 20, 14, NA),
                 assists=c(7, 8, 13, 13, 10, 6),
                 rebounds=c(8, 12, NA, NA, 5, 8))

#view data frame
df

  points assists rebounds
1     30       7        8
2     22       8       12
3     19      13       NA
4     20      13       NA
5     14      10        5
6     NA       6        8

We can use the all() and any() functions to evaluate different expressions for the values in the “rebounds” column:

#check if all values are less than 10 in rebounds column
all(df$rebounds < 10, na.rm=TRUE)

[1] FALSE

#check if any values are less than 10 in rebounds column
any(df$rebounds < 10, na.rm=TRUE)

[1] TRUE

#check if there are any NA values in rebounds column
any(is.na(df$rebounds))

[1] TRUE

From the output we can see:

  • Not all values are less than 10 in the rebounds column.
  • At least one value is less than 10 in the rebounds column.
  • There is at least one NA value in the rebounds column.

Related: How to Use is.na in R (With Examples)

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Add a Column to a Data Frame in R
How to Add an Empty Column to a Data Frame in R
How to Sort a Data Frame by Column in R

Leave a Reply

Your email address will not be published.