Min and Max Function in R

This chapter is dedicated to min and max function in R

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)
output:
[1] 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)
output:
[1] 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)
output:
[1] “x”

Example of Max function in R for a data frame:

Lets use mtcars data frame to demonstrate max function in r

# max function in R for a data frame

max(mtcars$mpg)
output:
[1] 33.9

 

 

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)
output:
[1] -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)
output:
[1] -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)
output:
[1] “a”

 

Example of Min function in R for a data frame:

Lets use mtcars data frame to demonstrate min function in r

# min function in R for a data frame

min(mtcars$mpg)
output:
[1] 10.4

previous small min and max function in r                                                                                                                next small min and max function in r