R Max and Min
R provides functions max()
and min()
to find the maximum and minimum values in a numeric vector. These functions are essential for data analysis.
Key Topics
Finding Max and Min
The max()
and min()
functions are used to find the highest and lowest values in a vector.
# Finding max and min values
data <- c(10, 20, 5, 30, 15)
max_value <- max(data)
min_value <- min(data)
print(max_value)
print(min_value)
Output:
[1] 30
[1] 5
[1] 5
Code Explanation: The max()
function finds the largest value, and the min()
function finds the smallest value in the data
vector.
Using with NA Values
If the vector contains NA
values, use the na.rm = TRUE
argument to ignore them.
# Handling NA values
data_with_na <- c(10, 20, NA, 30, 15)
max_value <- max(data_with_na, na.rm = TRUE)
min_value <- min(data_with_na, na.rm = TRUE)
print(max_value)
print(min_value)
Output:
[1] 30
[1] 10
[1] 10
Code Explanation: The na.rm = TRUE
argument is used to ignore NA
values when finding the max and min.
Key Takeaways
- Use
max()
andmin()
to find the maximum and minimum values in a vector. - Handle
NA
values usingna.rm = TRUE
to avoid errors. - These functions are useful for basic data analysis and summarization.