*48*

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 #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 #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 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 min(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 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 c min(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 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 d apply(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

**Additional Resources**

How to Calculate Standard Deviation in R

How to Calculate the Range in R

How to Loop Through Column Names in R