# Min() and Max() Function in R

This chapter is dedicated to min and max function in R.  min function in R – min(), is used to calculate the minimum of vector elements or minimum of a particular column of a dataframe. minimum of a group can also calculated using min() function in R by providing it inside the aggregate function. max(), is used to calculate the maximum of vector elements or maximum of a particular column of a dataframe. maximum of a group can also calculated using max() function in R by providing it inside the aggregate function. row wise maximum and minimum is calculated using max() and min() function with the help of dplyr package. we also calculates column wise maximum and column wise minimum. lets see an example of each.

• min() function in R computes the minimum value of a vector or data frame.
• max() function in R computes the maximum value of a vector or data frame.
• column wise maximum and minimum of the dataframe using max() and min() function.
• Row wise maximum and minimum of the dataframe in R using max() and min() function.
• maximum and minimum value of the group is calculated using max() and min() along with aggregate() and dplyr packages.

#### Syntax for min and Max function in R:

min(x, na.rm = FALSE)

max(x, na.rm = FALSE)

•   x – is numeric or character vector
• na.rm – a logical indicating whether missing values should be removed.

#### Example of Max function in R:

```# max function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
max(x)
```
 12.345

#### Example of Max function in R with NA:

Max function doesn’t give desired output, If NAs are present in the vector. So it has to be handled by using na.rm=TRUE in max() function

```# max function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
max(x, na.rm=TRUE)
```
 12.345

#### Example of Max function in R with character vector:

```# max function in R for a character vector

y<-c("d","e","a","x")
max(y)
```
 “x”

#### Example of max() function in R dataframe:

Lets create the data frame to demonstrate max function – max() in r

```### create the dataframe
Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120),
Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3))
```

so the resultant dataframe will be #### maximum value of a column in R data frame using max() function : max() function takes the column name as argument and calculates the maximum value  of that particular column

```# max() function in R : maximum value of a column in data frame

```

so the resultant maximum value of the “Price” column will be

output:

 120

#### column wise maximum using max() function:

max() function is applied to the required column through mapply() function, so that it  calculates the maximum value of required columns as shown below.

```
# max() function in R : maximum value of multiple columns in data frame

```

so the resultant maximum value of “Price” and “Tax” columns will be #### Maximum value of the column by group using max() function

aggregate() function along with the max() function calculates the maximum value of a group. here maximum of “Price” column, for “Item_Group” is calculated.

```##### maximum value of the column by group
FUN=max)
```

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. maximum price for each group is calculated as shown below #### Row wise maximum using max() function along with dplyr

Row wise maximum is calculated with the help rowwise() function of dplyr package  and max() function as shown below

```## row wise max using dplyr
library(dplyr)

rowwise() %>%
mutate(
Total_price = max(c(Price,Tax))
)
```

row wise max of “Price” and “Tax” is calculated and  populated for each row as shown below #### Example of Min function in R:

```# min function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567)
min(x)
```
 -14.567

#### Example of Min function in R with NA:

Min function doesn’t give desired output, If NAs are present in the vector. so it has to be handled by using na.rm=TRUE in min() function

```# min function in R for a numeric vector

x <-c(1.234,2.342,-4.562,5.671,12.345,-14.567,NA)
min(x, na.rm=TRUE)
```
 -14.567

#### Example of Min function in R with character vector:

```# min function in R for a character vector

y<-c("d","e","a","x")
min(y)
```
 “a”

#### Example of min() function in R dataframe:

Lets create the data frame to demonstrate min function – min() in r

```### create the dataframe
Price = c(100,80,80,90,65,70,60,70,25,60,40,35,50,120),
Tax = c(2,4,5,6,2,3,5,1,3,4,5,6,4,3))
```

so the resultant dataframe will be minimum value of a column in R data frame using min() function :

min() function takes the column name as argument and calculates the minimum value  of that particular column

```# min() function in R : minimum value of a column in data frame

```

so the resultant minimum value of the “Price” column will be

output:

 25

column wise minimum using min() function: min() function is applied to the required column through mapply() function, so that it  calculates the minimum value of required columns as shown below.

```
# min() function in R : minimum value of multiple columns in data frame

```

so the resultant minimum value of “Price” and “Tax” columns will be Minimum value of the column by group using min() function

aggregate() function along with the min() function calculates the minimum value of a group. here minimum of “Price” column, for “Item_Group” is calculated.

```##### minimum value of the column by group
FUN=min)
```

Item_group has three groups “Dairy”,”Fruit” & “Vegetable”. minimum price for each group is calculated as shown below Row wise minimum using min() function along with dplyr

Row wise minimum is calculated with the help rowwise() function of dplyr package  and min() function as shown below

```## row wise min using dplyr

library(dplyr)

rowwise() %>%
mutate(
min_price = min(c(Price,Tax))
)
```

row wise min of “Price” and “Tax” is calculated and  populated for each row as shown below For further understanding of min() and max() function in R using dplyr one can refer the dplyr documentation