How to Use pmax & pmin in R (With Examples)

The pmax and pmin functions in R are useful for finding the maximum and minimum of two or more vectors or numbers. These functions take two or more arguments and returns a vector of the same length as the longest argument, with the maximum or minimum values taken from any of the supplied arguments. Examples of how to use the pmax and pmin functions in R are provided to illustrate their use.


You can use the pmax() and pmin() functions in R to find the parallel maximum and minimum values, respectively, across multiple vectors.

These functions uses the following basic syntax:

pmax(vector1, vector2, vector3, ...)
pmin(vector1, vector2, vector3, ...) 

The following examples show how to use these functions with both vectors and data frames.

Example 1: Use pmax and pmin with Vectors

Suppose we have the following three vectors in R:

#define three vectors
vector1 <- c(2, 2, 3, 4, 5, 6, 9)
vector2 <- c(1, 2, 4, 3, 3, 5, 4)
vector3 <- c(0, 4, 3, 12, 5, 8, 8)

We can use the pmax and pmin functions to find the maximum and minimum values at corresponding elements across all three vectors:

#find max value across vectors
pmax(vector1, vector2, vector3)

[1]  2  4  4 12  5  8  9

#find min value across vectors
pmin(vector1, vector2, vector3)

[1] 0 2 3 3 3 5 4

Here’s how to interpret the output:

  • The max value in the first position across all vectors was 2. The minimum value in the first position across all vectors was 0.
  • The max value in the second position across all vectors was 4. The minimum value in the second position across all vectors was 2.

And so on.

Example 2: Use pmax and pmin with Data Frame Columns

Suppose we have the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 steals=c(24, 22, 36, 33, 30),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 41))

#view data frame
df

  team steals assists rebounds
1    A     24      33       30
2    B     22      28       28
3    C     36      31       24
4    D     33      39       24
5    E     30      34       41

We can use the pmax and pmin functions to find the maximum and minimum values at corresponding elements across all three vectors:

#find max value across steals, assists, and rebounds columns
pmax(df$steals, df$assists, df$rebounds)

[1] 33 28 36 39 41

#find minimum value across steals, assists, and rebounds columns
pmin(df$steals, df$assists, df$rebounds)

[1] 24 22 24 24 30
  • The max value in the first row across the steals, assists, and rebounds columns was 33 and the minimum value was 24.
  • The max value in the second row across the steals, assists, and rebounds columns was 28 and the minimum value was 22.

And so on.

Note: If you have missing values in any of the vectors, simply use the following syntax to ignore NA’s when calculating the maximum or minimum:

pmax(vector1, vector2, vector3, na.rm=TRUE)
pmin(vector1, vector2, vector3, na.rm=TRUE)

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

 

x