# R min() and max()

**R feed**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R, we can find the minimum or maximum value of a vector or data frame.

We use the `min()`

and `max()`

function to find minimum and maximum value respectively.

- The
`min()`

function returns the minimum value of a vector or data frame. - The
`max()`

function returns the maximum value of a vector or data frame.

## Syntax of min() and max() in R

The syntax of the min() and max() function is

**For min()**

min(collection, na.rm = Boolean)

**For max()**

max(collection, na.rm = Boolean)

In both the syntax,

`collection`

– is a vector or data frame`na.rm`

(optional) – is a boolean value that indicates whether value should be kept or removed,

## Example 1: Use of min() in R

numbers <- c(2,4,6,8,10) # return minimum value present in numbers min(numbers) # 2 characters <- c("s", "a", "p", "b") # return alphabetically minimum value in characters min(characters) # "a"

**Output**

[1] 2 [1] "a"

Here,

`min(numbers)`

- returns the smallest number in`numbers`i.e.**2**`min(characters)`

- returns alphabetically minimum value in`characters`i.e.`"a"`

## Example 2: Use of max() in R

numbers <- c(2,4,6,8,10) # return largest value present in numbers max(numbers) # 10 characters <- c("s", "a", "p", "b") # return alphabetically maximum value in characters max(characters) # "s"

**Output**

[1] 10 [1] "s"

Here,

`max(numbers)`

- returns the largest number in`numbers`i.e.**10**`max(characters)`

- returns alphabetically maximum value in`characters`i.e.`"s"`

## min() and max() in R with NA Values

While working on a large data set, we may encounter `NA`

(Not Applicable) values in a vector.

In this case the `min()`

function doesn't give desired output if `NA`

is present. For example,

numbers <- c(2, NA, 6, 7, NA, 10) # return smallest value min(numbers) # NA

**Output**

[1] NA

Here, we get `NA`

as output. But that is not the desired output.

So we can handle this using `na.rm`

argument

numbers <- c(2, NA, 6, 7, NA, 10) # return smallest value min(numbers, na.rm = TRUE) # 2

**Output**

[1] 2

Here, we have used the `na.rm`

argument to handle `NA`

values.

By setting `na.rm`

to `TRUE`

, we have removed `NA`

before the computation. So the output will be **2** not `NA`

.

**Note**: Similar to `min()`

, we can use `max()`

with NA values too.

## min() and max() in a Data Frame

In R, we can use `min()`

and `max()`

to find minimum and maximum value in a certain column of a data frame. For example,

# Create a data frame dataframe1 <- data.frame ( Name = c("Juan", "Kay", "Jay", "Ray", "Aley"), Age = c(22, 15, 19, 30, 23), ID = c(101, 102, 103, 104, 105) ) # return maximum value of Age column of dataframe1 print(max(dataframe1$Age)) # 30 # return minimum value of ID column of dataframe1 print(min(dataframe1$ID)) # 101

**Output**

[1] 30 [1] 101

Here, we have used the `max()`

and `min()`

function to find the maximum and minimum value of the `Age` and `ID` column respectively.

`max(dataframe1$Age)`

- returns the maximum value from the`Age`column of`dataframe1`i.e.**30**.`min(dataframe1$ID)`

- returns the minimum value from the`ID`column of`dataframe1`i.e.**101**.

**leave a comment**for the author, please follow the link and comment on their blog:

**R feed**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.