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.
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.
max(input, na.rm = FALSE)
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. |
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.
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.
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
To get the max value from the whole data frame, pass the data frame to this method.
df <- data.frame(
"col1" = c(21, 19, 11),
"col2" = c(18, 46, 10),
"col3" = c(5, 15, 25)
)
max(df)
# Output: [1] 46
If you want to find the maximum value between specific columns, pass those columns to this function.
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
Pass the row’s index to get the maximum value of that exact row of the data frame.
df <- data.frame(
"col1" = c(21, 19, 11),
"col2" = c(18, 46, 10),
"col3" = c(5, 15, 25)
)
max(df[2, ])
# Output: [1] 46
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.
data(mtcars)
max(mtcars)
# Output: [1] 472
The min() function finds the smallest value in a given set of numbers in a vector or data frame.
min(input, na.rm = FALSE)
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. |
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
If you add the NA value as missing data to the vector, it will return the NA value as a minimum value.
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.
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
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"
To find a specific column’s minimum value, pass that specific column to the min() method.
df <- data.frame(
"col1" = c(21, 19, 11),
"col2" = c(18, 46, 10),
"col3" = c(5, 15, 25)
)
min(df$col3)
# Output: [1] 5
To find the minimum value across all the df columns, pass the df to this method.
df <- data.frame(
"col1" = c(21, 19, 11),
"col2" = c(18, 46, 10),
"col3" = c(5, 15, 25)
)
min(df)
# Output: [1] 5
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
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.
df <- data.frame(
"col1" = c(21, 19, 11),
"col2" = c(18, 46, 10),
"col3" = c(5, 15, 25)
)
min(df[2, ])
# Output: [1] 15
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…
The as.Date() function in R converts various types of date and time objects or character…
The pnorm() function in R calculates the cumulative density function (cdf) value of the normal…
You may want to convert a vector to a string when you need to combine…
Set the current working directory The setwd() function sets the working directory to the new…
The sd() function in R calculates the sample standard deviation of a numeric vector or…