Table of Contents
pmax and pmin are useful functions in R that allow users to find the maximum and minimum values between two or more vectors. These functions are commonly used in data analysis and statistical calculations.
In R, pmax takes multiple vectors as input and returns a single vector with the maximum value for each corresponding element. Similarly, pmin returns the minimum value for each element. These functions are particularly useful when working with large datasets and needing to find the highest or lowest values within a certain range.
For example, suppose we have two vectors, x and y, representing the heights of students in inches and centimeters respectively. We can use pmax to find the maximum height between the two vectors, which would be the tallest student overall. This can be done by simply typing pmax(x, y) in the R console.
Another use case for pmax and pmin is in filtering data. For instance, if we have a dataset of sales data and we want to find the highest and lowest sales values for each month, we can use pmax and pmin to do so. This can help us identify the most successful and least successful months in terms of sales.
In summary, pmax and pmin are valuable tools in R for finding the maximum and minimum values between vectors. They are commonly used in data analysis, statistical calculations and filtering data.
Use pmax & pmin in R (With Examples)
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 41We 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)
Additional Resources
Cite this article
stats writer (2024). What are the uses of pmax and pmin in R, and can you provide examples of how they are used?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-are-the-uses-of-pmax-and-pmin-in-r-and-can-you-provide-examples-of-how-they-are-used/
stats writer. "What are the uses of pmax and pmin in R, and can you provide examples of how they are used?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/what-are-the-uses-of-pmax-and-pmin-in-r-and-can-you-provide-examples-of-how-they-are-used/.
stats writer. "What are the uses of pmax and pmin in R, and can you provide examples of how they are used?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-are-the-uses-of-pmax-and-pmin-in-r-and-can-you-provide-examples-of-how-they-are-used/.
stats writer (2024) 'What are the uses of pmax and pmin in R, and can you provide examples of how they are used?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-are-the-uses-of-pmax-and-pmin-in-r-and-can-you-provide-examples-of-how-they-are-used/.
[1] stats writer, "What are the uses of pmax and pmin in R, and can you provide examples of how they are used?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. What are the uses of pmax and pmin in R, and can you provide examples of how they are used?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
