What is the pmax() function in R?
Overview
The pmax() function in R obtains the parallel maxima of two or more given vectors. In simpler words, the function compares the values of the two vectors in pairs and returns a final vector that contains the maximum values from each pair.
Syntax
pmax(..., na.rm = FALSE)
Syntax for the pmax() function in R
Parameters
The pmax() function takes the following parameter values:
...: This represents the vector objects. It is a required parameter.na.rm: This takes a logical value (TRUE or FALSE) that indicates whether the function should remove missing values. It is an optional parameter.
Return value
The pmax() function returns a vector with the longest of the input vectors representing the pair-wise maxima of the input vectors.
Example
# Creating a vectora <- c(10, 8, 3, 9, 0, 5)b <- c(15, 4, 6, 9, 8, 4)# Obtaining the maximum values pair-wisepmax(a, b)
Explanation
- Lines 2–3: We create vector objects
aandb. - Line 6: We call the
pmax()function and pass our input vectors (aandb) to it. The function returns our desired vector (The maximum values from each pairing).