How can the min and max functions be used in R, and what are some examples of their implementation?

How can the min and max functions be used in R, and what are some examples of their implementation?

The min and max functions are useful tools in R for finding the smallest and largest values in a given set of data. These functions can be used to quickly and efficiently identify extreme values, which can be helpful in statistical analysis and data manipulation.

To use the min and max functions in R, simply input the desired data set as an argument within the function. The min function will return the smallest value in the data, while the max function will return the largest value. These functions can be applied to various types of data, including numerical, character, and logical.

For example, if we have a vector of numbers (1, 3, 5, 2, 7), the min function would return 1 and the max function would return 7. Similarly, if we have a vector of letters (“a”, “c”, “b”, “f”), the min function would return “a” and the max function would return “f”.

In addition to basic data types, the min and max functions can also be used with more complex data structures such as matrices and data frames. In these cases, the functions will return the minimum or maximum value across the entire data structure, rather than within a single vector.

Overall, the min and max functions are powerful tools for quickly finding the smallest and largest values in a data set. Their implementation can greatly assist in data analysis and manipulation, making them essential functions for any R programmer.

Use Min and Max Functions in R (With Examples)


You can use the min() and max() functions in R to quickly calculate the minimum and maximum values in a vector.

#find minimum value
min(x)

#find maximum value
max(x)

The following examples show how to use these functions in practice.

Example 1: Max & Min of Vector

The following code shows how to find the minimum and maximum values of a vector:

#define vector
x <- c(2, 3, 4, 4, 7, 12, 15, 19, 22, 28, 31, 34)

#find minimum value
min(x)

[1] 2
#find maximum value
max(x)

[1] 34

Note that if you have missing values in the vector, you must specify na.rm=TRUE to ignore missing values when calculating the minimum and maximum:

#define vector with some missing values
x <- c(2, 3, 4, 4, NA, 12, NA, 19, 22, 28, 31, 34)

#find minimum value
min(x, na.rm=TRUE)

[1] 2

#find maximum value
max(x, na.rm=TRUE)

[1] 34

Example 2: Max & Min of Entire Data Frame

The following code shows how to find the minimum and maximum values of an entire data frame:

#define data frame
df <- data.frame(a=c(1, 3, 4, 6, 8, 9),
                 b=c(7, 8, 8, 7, 13, 16),
                 c=c(11, 13, 13, 18, 19, 22),
                 d=c(12, 16, 18, 22, 29, 38))

#find minimum valuemin(df)

[1] 1

#find maximum value
max(df)

[1] 38

Example 3: Max & Min of Column in Data Frame

The following code shows how to find the minimum and maximum values of a specific column in a data frame:

#define data frame
df <- data.frame(a=c(1, 3, 4, 6, 8, 9),
                 b=c(7, 8, 8, 7, 13, 16),
                 c=c(11, 13, 13, 18, 19, 22),
                 d=c(12, 16, 18, 22, 29, 38))

#find minimum value of column cmin(df$c)

[1] 11

#find maximum value of column c
max(df$c)

[1] 22

Example 4: Max & Min of Several Columns in Data Frame

The following code shows how to find the minimum and maximum values of several columns in a data frame:

#define data frame
df <- data.frame(a=c(1, 3, 4, 6, 8, 9),
                 b=c(7, 8, 8, 7, 13, 16),
                 c=c(11, 13, 13, 18, 19, 22),
                 d=c(12, 16, 18, 22, 29, 38))

#find minimum value in columns a, b, and dapply(df[ , c('a', 'b', 'd')], 2, min)

 a  b  d 
 1  7 12 

#find maximum value in columns a, b, and d
apply(df[ , c('a', 'b', 'd')], 2, max)

 a  b  d 
 9 16 38 

Cite this article

stats writer (2024). How can the min and max functions be used in R, and what are some examples of their implementation?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-the-min-and-max-functions-be-used-in-r-and-what-are-some-examples-of-their-implementation/

stats writer. "How can the min and max functions be used in R, and what are some examples of their implementation?." PSYCHOLOGICAL SCALES, 2 May. 2024, https://scales.arabpsychology.com/stats/how-can-the-min-and-max-functions-be-used-in-r-and-what-are-some-examples-of-their-implementation/.

stats writer. "How can the min and max functions be used in R, and what are some examples of their implementation?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-the-min-and-max-functions-be-used-in-r-and-what-are-some-examples-of-their-implementation/.

stats writer (2024) 'How can the min and max functions be used in R, and what are some examples of their implementation?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-the-min-and-max-functions-be-used-in-r-and-what-are-some-examples-of-their-implementation/.

[1] stats writer, "How can the min and max functions be used in R, and what are some examples of their implementation?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can the min and max functions be used in R, and what are some examples of their implementation?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top