R max() and min() Functions

max()

The max() function in R finds the maximum value of a vector or data frame. For example,  if you have vec <- c(21, 19, 11, 2, 18, 3), then max(vec) will return 21.

Finding the maximum value of a vector

This function can be used with any data type that is coercible to a numeric type, including logical vectors where TRUE is treated as 1 and FALSE as 0.

Syntax

max(input, na.rm = FALSE)

Parameters

Argument Description
input It represents an R object such as a vector or data frame.
na.rm It removes NA values; if it mentions FALSE, it considers NA, and if it mentions TRUE, it removes NA from the vector or data frame.

Finding the maximum value of a vector

Let’s define a vector and find the maximum value from it.

rv <- c(19, 21, -20, -46, 29)

cat("The maximum value of thev vector is:", max(rv))

# Output: The maximum value of thev vector is: 29

You can see from the vector that mathematically, the maximum value is 29, and we get the same output.

Ignoring NA values

If you add the NA value as missing data to the vector, it will return the NA value as a maximum value because it is not what the NA value represents.

Use the max() function to a Vector containing NA values

rv <- c(19, 21, -20, NA, 29)

cat("The maximum value of thev vector is:", max(rv))

# Output: The maximum value of thev vector is: NA

But don’t worry, and there is an easy solution! Just specify the option na.rm = TRUE within the max() function.

rv <- c(19, 21, -20, NA, 29)

cat("The maximum value of thev vector is:", max(rv, na.rm = TRUE))

# Output: The maximum value of thev vector is: 29

Using max() function on data frame

To get the max value from the whole data frame, pass the data frame to this method.

Using max() function on data frame

df <- data.frame(
  "col1" = c(21, 19, 11),
  "col2" = c(18, 46, 10),
  "col3" = c(5, 15, 25)
)

max(df)

# Output: [1] 46

Finding the maximum value between two columns

If you want to find the maximum value between specific columns, pass those columns to this function.

Finding the maximum value between two columns

df <- data.frame(
  "col1" = c(21, 19, 11),
  "col2" = c(18, 46, 10),
  "col3" = c(5, 15, 25)
)

max(c(df$col1, df$col3))

# Output: [1] 25

Finding the maximum of the row

Pass the row’s index to get the maximum value of that exact row of the data frame.

Figure of Finding the Maximum of the row using the max() Function

df <- data.frame(
  "col1" = c(21, 19, 11),
  "col2" = c(18, 46, 10),
  "col3" = c(5, 15, 25)
)

max(df[2, ])

# Output: [1] 46

Finding a global max value of a specific column of a dataset

Import the specific dataset and select the specific column using $ operator. Then, find out its maximum value.

data(ChickWeight)
val <- max(ChickWeight$weight)
cat("The maximum value of weight column is:", val)

# Output: The maximum value of weight column is: 373

That means the maximum weight of the ChickWeight dataset is 373.

Finding the maximum global value of the dataset

data(mtcars)
max(mtcars)

# Output: [1] 472

min()

The min() function finds the smallest value in a given set of numbers in a vector or data frame.

Figure of Finding a minimum value of a vector

Syntax

min(input, na.rm = FALSE)

Parameters

Argument Description
input It is a vector or a data frame.
na.rm It removes NA values; if it mentions FALSE, it considers NA, and if it mentions TRUE, it removes NA from the vector or data frame.

    Finding a minimum value of a vector

    Let’s define a vector with positive and negative values and find the minimum value.

    rv <- c(19, 21, -20, -46, 29)
    
    cat("The minimum value of thev vector is:", min(rv))
    
    # Output: The minimum value of thev vector is: -46

    Dealing with NA Values

    If you add the NA value as missing data to the vector, it will return the NA value as a minimum value.

    Figure of If a vector contains NA values

    rv <- c(19, 21, -20, -46, NA)
    
    cat("The minimum value of thev vector is:", min(rv))
    
    # Output: The minimum value of thev vector is: NA

    But don’t worry, and there is an easy solution! Just specify the option na.rm = TRUE.

    Passing na.rm = TRUE

    rv <- c(19, 21, -20, -46, NA)
    
    cat("The minimum value of thev vector is:", min(rv, na.rm = TRUE))
    
    # Output: The minimum value of thev vector is: -46

    Usage with character vector

    If you are using character vectors, the min() function will consider alphabetical order and return the minimum value according to that.

    character_vector <- c("Tata", "Azad", "Shelter", "Credo", "Ireda")
    
    min(character_vector)
    
    # Output: [1] "Azad"

    Finding a minimum value of a single data frame column

    To find a specific column’s minimum value, pass that specific column to the min() method.

    Figure of Finding a minimum value of a single data frame column

    df <- data.frame(
      "col1" = c(21, 19, 11),
      "col2" = c(18, 46, 10),
      "col3" = c(5, 15, 25)
    )
    
    min(df$col3)
    
    # Output: [1] 5

    Finding a minimum value across all data frame columns

    To find the minimum value across all the df columns, pass the df to this method.

    Figure of Finding a minimum value across all data frame columns

    df <- data.frame(
      "col1" = c(21, 19, 11),
      "col2" = c(18, 46, 10),
      "col3" = c(5, 15, 25)
    )
    
    min(df)
    
    # Output: [1] 5

    Finding the minimum value between two columns

    Figure of Finding the minimum value between two columns.

    df <- data.frame(
      "col1" = c(21, 19, 11),
      "col2" = c(18, 46, 10),
      "col3" = c(5, 15, 25)
    )
    
    min(c(df$col1, df$col2))
    
    # Output: [1] 10

    Finding the minimum value of a specific row

    You can find the minimum value of a specific row by passing the index of the row using the square brackets “([ ])” of a data frame.

    Finding the Minimum of the row using the min() Function

    df <- data.frame(
      "col1" = c(21, 19, 11),
      "col2" = c(18, 46, 10),
      "col3" = c(5, 15, 25)
    )
    
    min(df[2, ])
    
    # Output: [1] 15

    Finding the minimum value of a specific column of a dataset

    We can apply the min() function to that specific column by using the $ operator with the name of the data.

    data(ChickWeight)
    
    val <- min(ChickWeight$weight)
    
    cat("The minimum value of weight column is:", val)
    
    # Output: The minimum value of weight column is: 35

    That’s it!

    Leave a Comment