Chapter 44: R Max and Min

1. What do max() and min() actually do?

  • max() → returns the largest value in a vector (or across multiple vectors)
  • min() → returns the smallest value in a vector (or across multiple vectors)

They are vectorized — they work beautifully on entire columns/vectors at once (this is R’s superpower).

They also have very important arguments: na.rm = TRUE and … (multiple inputs).

2. Basic Usage – Copy-Paste These Right Now

R

Golden rule 2026: Always use na.rm = TRUE unless you deliberately want to know there is missing data.

3. Real-Life Examples – How People Actually Use max() & min()

Example 1 – Find highest & lowest value in a column

R

Example 2 – Range of values (max – min)

R

Example 3 – Find extreme rows in data frame

R

Example 4 – Clamp / Winsorize values (very common in data cleaning)

R

→ pmax() and pmin() are parallel/element-wise versions — very useful!

4. Multiple Inputs – Comparing Several Vectors

R

→ Useful when you want overall min/max across several groups.

5. Important Arguments & Edge Cases (2026 Reality)

Argument What it does Default Almost always use?
na.rm Remove NA before calculation FALSE YES – almost always
Multiple vectors/objects Yes for comparison
finite (in newer R) Ignore Inf / -Inf FALSE Sometimes

Edge cases you will meet:

R

Rule: If na.rm = TRUE and all values are NA → returns -Inf/Inf + warning. Always check length or use ifelse() / if (all(is.na(x))) …

6. Modern 2026 Nice-to-Haves

R

Your Mini Practice Right Now

Copy & run — then try changing columns:

R

Now try:

  • Find car with highest horsepower (hp)
  • Use na.rm = TRUE on a vector with NA
  • Use pmax() to cap values at 25

You just used max()/min() in real analysis style!

Feeling good?

Next questions?

  • Want to combine max/min with ifelse() for conditional capping?
  • Learn range(), quantile(), IQR() next?
  • Or move to summary statistics in detail (mean, median, sd, var)?

Just tell me — whiteboard is ready! 📊🧮🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *