The max() is a built-in R function that takes the R object as an input and returns the maximum value out of it. For example, if you have a vector vec <- c(21, 19, 11, 2, 18, 3), then max(vec) will return 21.
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 mentions 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.
Example 1: How to find the maximum value of a Vector in R
You can use the max() function to find a maximum value of a 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.
Example 2: Applying the max() function to a Vector containing 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 is. 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, and there is an easy solution! Just specify the option na.rm = TRUE within the 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.
Example 3: Using the 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)
head(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
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.
Example 4: Find the 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: 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
Example 5: Finding the maximum value between two columns.
We will use the mtcars dataset to find the maximum value between the two columns. For example, we will find the maximum value of the columns mpg and cyl of mtcars.
data(mtcars)
max(c(mtcars$mpg, mtcars$cyl))
Output
[1] 33.9
Example 6: Finding the maximum global 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
[1] 472
In our example, the maximum value of the mtcars data frame is 472.
Example 7: Finding 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
[1] 167.6
Example 8: Finding 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
[1] "TV Show is Happening"
Conclusion
The max() function in R is a built-in function that finds the maximum value of a vector or data frame. It takes an 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, you can use the max() function.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.