# max() Function in R : How to Find Maximum Value of Vector

The max() function in R computes the maximum value of a vector. The min() function in R computes the minimum value of a vector. The maximum of the group is also calculated using the max() function.

## max() in R

The max() is a built-in R function that finds the maximum value of the vector or data frame. It takes the R object as an input and returns the maximum value out of it.

To find the maximum value of vector elements, data frame, and columns, use the max() function.

### Syntax

``max(input, na.rm = FALSE)``

### Parameters

input: It is a  Vector or a data frame.

na.rm: It removes the NA values; if it mentioned FALSE, it considers NA, or if it mentioned TRUE, it removes NA from the vector or a data frame.

### Return Value

The max() function returns the maximum value of a vector or data frame.

## Applying max() function to Vector in R

Define a vector and pass that vector to the max() function to find the maximum element of the vector. You can define a vector using the c() function.

``````data <- c(19, 21, -20, -46, 29)
cat("The maximum value of thev vector is:", max(data))``````

#### 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.

## Applying max() function to a Vector containing NA values

If you add 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 is because it can be anything. So it will assume the maximum value as NA returns that in the output.

``````data <- c(19, 21, -20, NA, 29)
cat("The maximum value of thev vector is:", max(data))``````

#### Output

``The maximum value of thev vector is: NA``

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

``````data <- c(19, 21, -20, NA, 29)
cat("The maximum value of thev vector is:", max(data, na.rm = TRUE))``````

#### Output

``The maximum value of thev vector is: 29``

You can see that it has completely ignored the NA value, and it returns 29 as the maximum value.

## Applying max() function to a data set

Let’s use the ChickWeight dataset and check out its first 6 rows using the head() function.

``````data(ChickWeight)

#### Output

``````   weight Time Chick Diet
1    42     0    1    1
2    51     2    1    1
3    59     4    1    1
4    64     6    1    1
5    76     8    1    1
6    93    10    1    1``````

If we want to calculate the maximum value of one column, we can apply the max and min functions to that specific column with the name of the data, the \$-sign, and the column’s name.

Let’s say we want to calculate the maximum value of the weight column from the whole dataset, and then you can use the max() function and pass the weight column.

``````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.

## Find maximum value across all columns using max()

You can find the maximum value of all the columns of your data matrix by using the sapply() function. The sapply() function in R works like lapply(), but it tries to interpret the output to the most fundamental data structure possible, which is either Vector or Matrix.

Let’s use the mtcars dataset and find the maximum value across all the columns.

``````data(mtcars)
sapply(mtcars, max)``````

#### Output

`````` mpg    cyl    disp    hp     drat    wt   qsec    vs     am    gear
33.900 8.000 472.000 335.000 4.930   5.424 22.900 1.000  1.000  5.000

carb
8.000``````

## Find the maximum value between two columns.

We will use the mtcars dataset and find the maximum value between two columns. We will find the maximum value of the columns mpg and cyl of mtcars.

``````data(mtcars)
max(c(mtcars\$mpg, mtcars\$cyl))``````

#### Output

`` 33.9``

## Find the global maximum value of the data frame.

The computation of the global max value of a data frame is straightforward. Just apply the max() function and pass the data frame as an argument that returns the max value.

``````data(mtcars)
max(mtcars)``````

#### Output

`` 472``

In our example, the maximum value of mtcars data frame is 472.

## Find the Maximum of the row using the max() Function.

You can find the maximum value of the data frame row using the following code.

``````data(mtcars)
max(mtcars[10, ])``````

#### Output

`` 167.6``

## Find the maximum character of String.

We can use the max() function to define the maximum of strings in alphabetic order.

``````data <- c("Harry Potter",
"TV Show is Happening",
"At HBOMAX",
"It is in early stage")

max(data)``````

#### Output

`` "TV Show is Happening"``

In our example vector, it contains info about the upcoming harry potter live-action show. If we want to check which of these strings is the last one in the alphabet, we have applied the max() function.

That is it for max() function in R programming.

Categories R