In this article, you will learn how to lớn use min và max in R. I’m going lớn explain both functions in the same tutorial, since the R syntax of the two functions is exactly the same.

Bạn đang xem: The theory of max

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 và min functions is shown above. In the following R tutorial, I’m going khổng lồ show you eight examples for the application of max và min in the R programming language.

Let’s dive into it…

## Example 1: Apply max & min lớn Vector in R

The most basic usage of max và min is their application to lớn a numeric vector. Let’s create an example vector first:

 x1 c(4, 1, - 50, 20, 8) # Create example vector
x1 (x1) # Apply max khổng lồ vector# 20max(x1) # Apply max to lớn 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 khổng lồ vector# -50
min(x1) # Apply min to vector# -50

The minimum value of our vector is – 50.

By the way: I have also recorded a đoạn clip containing Examples 1 và 2 of this tutorial. You can kiểm tra out the video clip tutorial here:

Please accept YouTube cookies lớn play this video. By accepting you will be accessing nội dung from YouTube, a service provided by an external third party.

Table 1: The mtcars Data as Example data.frame for the Application of max() & min().

Each row of the mtcars data mix consists of one car và the columns of the data contain different information on each oto (mpg = miles per gallon; cyl = cylinder; and so on…).

If we want lớn calculate the maximum & minimum of one column, we can apply the max & min algorithms to this column with the name of the data, the \$-sign, and the name of the column. Let’s vì chưng this in practice:

 max(mtcars\$mpg) # Compute max of column mpg# 33.9
max(mtcars\$mpg) # Compute max of column mpg# 33.9

Same for min:

 min(mtcars\$mpg) # Compute max of column mpg# 10.4
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 & minima of all the columns of your data matrix. Of cause, you could apply the max & min R functions lớn each of the columns one by one. However, the sapply function provides a much smoother và automatized way khổng lồ 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
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
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 & minimum of the whole data set? So be it…

## Example 5: Global max & min of Data Frame

The computation of the global max và 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 lớn whole data.frame# 472
max(mtcars) # Apply max algorithm to lớn whole data.frame# 472

The global maximum of mtcars is 472…

 min(mtcars) # Apply min algorithm lớn whole data.frame# 0
min(mtcars) # Apply min algorithm to lớn 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, & the global minimum of zero is in both vs & am.

## Example 6: max & min Between Two Columns

Another situation where max & min can be helpful is when you want khổng lồ know the max & min between two columns or vectors.

Let’s assume that we want to know the maximum & minimum value of the columns mpg & 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
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
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 và minima of rows?

## Example 7: Maximum & Minimum of Row

Sure, the typical application of max and min is khổng lồ 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
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
min(mtcars<5,>) # Compute min of one row# 0

… và 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 và minimum is useful for numbers and that’s it, correct?

Nope, that’s wrong!

We can also use max và min lớn determine minima or maxima of strings in an alphabetic order. We can vị that by simply inserting a character vector (or column or row) between the parentheses of the max và min functions.

 x_char c("hello", # Create character vector "R is nice", "max and min functions are awesome", "aaaaaa")
x_char (x_char) # Apply max lớn character vector# "R is nice"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 khổng lồ character vector# aaaaaa
min(x_char) # Apply min khổng lồ 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 khổng lồ find the highest & lowest values in R. However, there are many R functions that can be used in a similar fashion. If you want to lớn learn more about that, I can recommend the following YouTube đoạn clip of the BIO-RESEARCH channel.

Xem thêm: Nhạc Sĩ Phạm Tuyên Và Những Bài Hát Thiếu Nhi Nổi Tiếng, Những Bài Hát Của Nhạc Sĩ Phạm Tuyên

By accepting you will be accessing content from YouTube, a service provided by an external third party.

January 20, 2022 11:47 pm

January 21, 2022 10:20 pm