R Programming language - R min() and max()

www.ig‮i‬ftidea.com

The min() and max() functions in R are used to find the minimum and maximum values in a vector or a set of values.

The min() function returns the smallest value in a vector or a set of values, while the max() function returns the largest value.

Here are some examples of how to use min() and max() in R:

# Create a vector of numbers
x <- c(5, 2, 8, 4, 1)

# Find the minimum value in the vector
min(x)
# Output: 1

# Find the maximum value in the vector
max(x)
# Output: 8

# Find the minimum and maximum values in a set of numbers
min(5, 2, 8, 4, 1)
# Output: 1

max(5, 2, 8, 4, 1)
# Output: 8

You can also use the na.rm argument to exclude any missing values (NA) from the calculation. For example:

# Create a vector with missing values
y <- c(5, 2, 8, NA, 4, 1)

# Find the minimum value in the vector, excluding missing values
min(y, na.rm = TRUE)
# Output: 1

# Find the maximum value in the vector, excluding missing values
max(y, na.rm = TRUE)
# Output: 8

In addition to vectors and sets of numbers, you can also use min() and max() on other R data structures such as matrices, data frames, and lists. In these cases, you can specify which column or element to find the minimum or maximum value of.