# The pmax and pmin R Functions | 3 Examples (How to Handle Warnings & NA)

In this tutorial, I will show you how to use the **R functions pmax() and pmin()**. I’ll explain both functions in the same article, since the R syntax of the two functions is exactly the same.

**Basic R Syntax:**

pmax(x1, x2) pmin(x1, x2) |

pmax(x1, x2) pmin(x1, x2)

The **R pmax function** returns the **parallel maxima** of two or more input vectors.

The **R pmin function** returns the **parallel minima** of two or more input vectors.

The basic R syntax for the pmax and pmin functions is illustrated above. In the following R tutorial, I’m going to show you **three examples** for the usage of pmax and pmin in R.

Let’s get started…

## Example 1: Basic Application of pmax & pmin in R

Typically pmax and pmin are applied to two numeric vectors with the same length. Let’s create two example vectors:

x1 <- c(2, 8, 3, 4, 1, 5) # First example vector x2 <- c(0, 7, 5, 5, 6, 1) # Second example vector |

x1 <- c(2, 8, 3, 4, 1, 5) # First example vector x2 <- c(0, 7, 5, 5, 6, 1) # Second example vector

Our vectors are named *x1* and *x2* and both of them consist of six numeric values.

Now, let’s apply the pmax R function to these vectors:

pmax(x1, x2) # Application of pmax in R # 2 8 5 5 6 5 |

pmax(x1, x2) # Application of pmax in R # 2 8 5 5 6 5

As you can see, the R (or RStudio) output is a vector of the same length as the two input vectors (not as in case of max() and min(), were only one value is returned). Each of the numbers of this output vector is the parallel maximum of the two input vectors, i.e. 2 > 0; 8 > 7; 5 > 3; 5 > 4; 6 > 1; and 5 > 1.

The same works with the pmin R function. The only difference is that pmin calculates the parallel minimum of each pair of values:

pmin(x1, x2) # Application of pmin in R # 0 7 3 4 1 1 |

pmin(x1, x2) # Application of pmin in R # 0 7 3 4 1 1

Looks good. So let’s move on to the next example…

## Example 2: Warning! “an argument will be fractionally recycled”

A common R warning message returned by pmax and pmin is the following:

*Warning message:
In pmax(x, y) : an argument will be fractionally recycled*

I’ll show you why and when this happens. First, let’s create a new example vector:

x3 <- c(x2, 9, 3) # Third example vector (longer) x3 # Print example vector to RStudio console # 2 8 5 5 6 5 9 8 |

x3 <- c(x2, 9, 3) # Third example vector (longer) x3 # Print example vector to RStudio console # 2 8 5 5 6 5 9 8

As you can see, the new example vector (called *x3*) consists of the same values as the previously used example vector *x2*, but with two additional values. Now, let’s apply pmax to *x1* (created in Example 1) and the new vector *x3*:

pmax(x1, x3) # pmax for vectors with different length # 2 8 5 5 6 5 9 8 |

pmax(x1, x3) # pmax for vectors with different length # 2 8 5 5 6 5 9 8

**Figure 1: pmax() Warning Message for Vectors with Different Length.**

We got an output – that’s good. However, we also got a warning message. Why?

The reason is that out two input vectors have a different length. While *x3* consists of eight values, *x1* contains only of six values.

For that reason, *x1* has to be recycled to have the same length as *x3*, i.e. the first two values of *x1* are compared with the last two values of *x3*.

Of cause, the same applies to pmin – just with recycled minima instead of maxima:

pmin(x1, x3) # pmin for vectors with different length # 0 7 3 4 1 1 2 3 |

pmin(x1, x3) # pmin for vectors with different length # 0 7 3 4 1 1 2 3

That was basically the most confusing part for the pmax and pmin R commands. In the next example, I show you another typical issue with the two functions…

## Example 3: pmax & pmin Return NA

For the next example, let’s create a new vector:

x4 <- x2 # Fourth example vector (with NA) x4[1] <- NA # Insert missing value at first position x4 # Print example vector to RStudio console # NA 7 5 5 6 1 |

x4 <- x2 # Fourth example vector (with NA) x4[1] <- NA # Insert missing value at first position x4 # Print example vector to RStudio console # NA 7 5 5 6 1

As you can see, our new vector *x4* contains the same numbers as *x2*, but the first value is NA. In R, NA stands for Not Available, i.e. the value is missing.

If we apply pmax to *x1* (as created in Example 1) and the new vector *x4*, we get the following:

pmax(x1, x4) # Apply pmax to vector with NA # NA 8 5 5 6 5 |

pmax(x1, x4) # Apply pmax to vector with NA # NA 8 5 5 6 5

Not only the first entry of *x4* is NA. The first entry of the RStudio console output is NA as well.

The same happens, when we apply the R pmin function to these two vectors:

pmin(x1, x4) # Apply pmin to vector with NA # NA 7 3 4 1 1 |

pmin(x1, x4) # Apply pmin to vector with NA # NA 7 3 4 1 1

OK, so what to do? Fortunately, there is an easy fix. Just specify *na.rm = TRUE* within the pmax…

pmax(x1, x4, na.rm = TRUE) # Specify na.rm = TRUE # 2 8 5 5 6 5 |

pmax(x1, x4, na.rm = TRUE) # Specify na.rm = TRUE # 2 8 5 5 6 5

…and the pmin functions:

pmin(x1, x4, na.rm = TRUE) # Specify na.rm = TRUE # 2 7 3 4 1 1 |

pmin(x1, x4, na.rm = TRUE) # Specify na.rm = TRUE # 2 7 3 4 1 1

AS you can see, the NA values are ignored and the available value in *x1* (i.e. 2) is returned by both functions.

Easy peasy! But be aware that there are more options for the handling of NA – and that’s what I’m going to show you next…

## Video: Replace NA Values with Zero

In the previous example, I showed you how to remove NA values within the pmax and pmin functions via *na.rm = TRUE*. However, in your specific situation it might be better to replace NA values with other values, before the application of pmax or pmin. Have a look at the following video of my *Statistical Programming YouTube channel*. In the video, I’m showing you how to replace NA with 0 in R:

## Further Reading

- The max & min Functions in R
- The R length Function
- The R is.na Function
- NA Values in R
- Replace NA by Zero in R
- The R Programming Language

### Subscribe to my free statistics newsletter: