R max and min Functions | 8 Examples: Remove NA Value, Two Vectors, Column & Row
In this article, you will learn how to use min and max in R. I’m going to explain both functions in the same tutorial, since the R syntax of the two functions is exactly the same.
Basic R Syntax:
The R max function returns the maximum value of a vector or column.
The R min function returns the minimum value of a vector or column.
The basic R code for the max and min functions is shown above. In the following R tutorial, I’m going to show you eight examples for the application of max and min in the R programming language.
Let’s dive into it…
Example 1: Apply max & min to Vector in R
The most basic usage of max and min is their application to a numeric vector. Let’s create an example vector first:
x1 <- c(4, 1, - 50, 20, 8) # Create example vector
Our example vector consists of five numbers, stored in the data object x1. Now, let’s compute the maximum and minimum of this vector.
The maximum can be computed with the following R code:
max(x1) # Apply max to vector # 20
As you can see in the RStudio console, the maximum of our vector is 20.
The same code works for the min function:
min(x1) # Apply min to vector # -50
The minimum value of our vector is – 50.
Easy peasy! But there can occur complications and that’s what I’m going to show you next…
Example 2: Vector with NA Value
A problem can occur, when your data contains NA values (i.e. missing data). For the second example, let’s add some missing data to our example vector:
x2 <- c(x1, NA) # Create example vector with NA x2 # Print vector to RStudio console # 4 1 -50 20 8 NA
Our new example vector looks exactly as in Example 1, but this time with an NA value at the end. Let’s see what happens when we apply max and min as before:
max(x2) # max returns NA # NA
The max function returns NA…
min(x2) # min also returns NA # NA
…and the min function does the same.
But don’t worry, there is an easy solution! Just specify the option na.rm = TRUE within the max and min functions:
max(x2, na.rm = TRUE) # Specify na.rm = TRUE # 20
As you can see, by using na.rm = TRUE we get the same maximum…
min(x2, na.rm = TRUE) # Specify na.rm = TRUE # -50
…and minimum as in Example 1.
But what if we want to apply max and min to a column of a data frame? You guessed it, that’s what I’m going to show you next.
Example 3: max() & min() of Column
For the next example, I’m going to use the mtcars data set. The data can be loaded with the following R code:
data("mtcars") # Load mtcars data in RStudio
Let’s have a look at the data:
head(mtcars) # First 6 rows of mtcars data frame
Table 1: The mtcars Data as Example data.frame for the Application of max() and min().
Each row of the mtcars data set consists of one car and the columns of the data contain different information on each car (mpg = miles per gallon; cyl = cylinder; and so on…).
If we want to calculate the maximum and minimum of one column, we can apply the max and min algorithms to this column with the name of the data, the $-sign, and the name of the column. Let’s do this in practice:
max(mtcars$mpg) # Compute max of column mpg # 33.9
Same for min:
min(mtcars$mpg) # Compute max of column mpg # 10.4
The maximum of the column mpg of the mtcars data frame is 33.9 and the minimum is 10.4.
Let’s automatize this code…
Example 4: Maxima & Minima Across All Columns
You might be interested in the maxima and minima of all the columns of your data matrix. Of cause, you could apply the max and min R functions to each of the columns one by one. However, the sapply function provides a much smoother and automatized way to calculate all maxima and minima with one line of code.
The maxima across all columns can be computed as follows…
sapply(mtcars, max) # Compute max of all columns # mpg cyl disp hp drat wt qsec vs am gear carb # 33.900 8.000 472.000 335.000 4.930 5.424 22.900 1.000 1.000 5.000 8.000
…and the minima across all columns can be computed as follows:
sapply(mtcars, min) # Compute min of all columns # mpg cyl disp hp drat wt qsec vs am gear carb # 10.400 4.000 71.100 52.000 2.760 1.513 14.500 0.000 0.000 3.000 1.000
So, what if you don’t care about a single column. You want to know the maximum and minimum of the whole data set? So be it…
Example 5: Global max & min of Data Frame
The computation of the global max and min of a data table is quite easy. Just apply max and min as we did in the previous examples – but this time insert the name of the whole data frame between the parentheses:
max(mtcars) # Apply max algorithm to whole data.frame # 472
The global maximum of mtcars is 472…
min(mtcars) # Apply min algorithm to whole data.frame # 0
…and the global minimum is zero.
By the way, if you compare these results with the results of Example 4, you can see that the global maximum of 472 is in the column disp, and the global minimum of zero is in both vs and am.
Example 6: max & min Between Two Columns
Another situation where max and min can be helpful is when you want to know the max and min between two columns or vectors.
Let’s assume that we want to know the maximum and minimum value of the columns mpg and cyl. We can calculate that with the following R codes for max…
max(c(mtcars$mpg, mtcars$cyl)) # Max between two columns / vectors # 33.9
…and with the following line of command for min:
min(c(mtcars$mpg, mtcars$cyl)) # Min between two columns / vectors # 4
OK looks good. I think that’s enough for columns – but what about maxima and minima of rows?
Example 7: Maximum & Minimum of Row
Sure, the typical application of max and min is to columns. But sometimes it might be useful to know the maximum or minimum of a row. We can compute that with the following R code:
max(mtcars[5,]) # Compute max of one row # 360
The maximum of row number five is 360…
min(mtcars[5,]) # Compute min of one row # 0
… and the minimum is zero.
Note: You can replace the number 5 with any row number you want.
You think that has proven the flexibility of min and max? Wait for the next example…
Example 8: Maximum & Minimum of Character String
The maximum and minimum is useful for numbers and that’s it, correct?
Nope, that’s wrong!
We can also use max and min to determine minima or maxima of strings in an alphabetic order. We can do that by simply inserting a character vector (or column or row) between the parentheses of the max and min functions.
As in the examples before, let’s start with some example data:
x_char <- c("hello", # Create character vector "R is nice", "max and min functions are awesome", "aaaaaa")
Our example vector contains some random words and phrases. If we want to check, which of these strings is the last one in the alphabet, we can apply the max function…
max(x_char) # Apply max to character vector # "R is nice"
…and if we want to examine, which string is the first in alphabetic order, we can use the R min function…
min(x_char) # Apply min to character vector # aaaaaa
Obviously, in this group of strings it’s aaaaaa.
Video: max, min & Similar Functions
In this tutorial, I showed you several ways how to use max and min in R. However, there are many R functions that can be used in a similar fashion. If you want to learn more about that, I can recommend the following YouTube video of the BIO-RESEARCH channel.