How to obtain the pairwise minimum of two or more vectors in R

Overview

In R, the pmin() function is used to obtain the parallel minima of two or more given vectors. In simple words, it returns the minimum of the values of the two or more vectors pair-wise.

Syntax

pmin(..., na.rm = FALSE)
Syntax for the pmin() function in R

Parameters

This function takes the following parameter values:

  • ...: This represents input vector objects. This parameter is required.
  • na.rm: This takes a logical value (TRUE or FALSE) indicating if the missing values should be removed or not. This parameter is optional.

Return value

This function returns a vector with the longest of the input vectors representing the pair-wise minima of the input vectors.

Example

# creating vector
a <- c(10, 8, 3, 9, 0, 5)
b <- c(15, 4, 6, 9, 8, 4)
# obtaining the maximum values pairwise
pmin(a, b)

Explanation

  • Line 2 and 3: We create vector objects, a and b.
  • Line 6: We obtain the minimum values in the input vector objects pairwise.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources