R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
The min()
function in R is a fundamental tool for retrieving the smallest value from a numeric object, such as vectors, matrices, or data frames. It provides an efficient way to identify the lowest value in your dataset.
In this tutorial, we'll explore how to use the min()
function with various R objects.
You can get the minimum value from a numeric vector directly using the min()
function.
vec <- c(5, 3, 9, 2, 7) # Get the minimum value min_val <- min(vec) print(min_val) # Outputs: 2
min()
with Missing Values (NA)If your object contains missing values (NA
), the min()
function will return NA
by default. To change this behavior and retrieve the smallest non-missing value, use the na.rm
argument.
vec_with_na <- c(5, 3, NA, 9, 2, 7) # Get the minimum value ignoring NA min_val <- min(vec_with_na, na.rm = TRUE) print(min_val) # Outputs: 2
min()
with MatricesFor matrices, the min()
function considers all elements and returns the smallest value.
mat <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2) # Get the minimum value min_val <- min(mat) print(min_val) # Outputs: 1
min()
with Data FramesWhen used with a data frame, the min()
function will consider all the columns and return the smallest value. If you want the minimum of a specific column, you should reference it.
df <- data.frame(A = c(1, 3, 5), B = c(4, 2, 6)) # Get the minimum value across the entire data frame min_val_all <- min(df, na.rm = TRUE) print(min_val_all) # Outputs: 1 # Get the minimum value from column A min_val_A <- min(df$A, na.rm = TRUE) print(min_val_A) # Outputs: 1
You can pass several numbers directly to the min()
function, and it will return the smallest among them.
min_val <- min(5, 7, 2, 8, 3) print(min_val) # Outputs: 2
min()
with Character VectorsWhile its primary purpose is with numeric data, the min()
function can also be used with character vectors, where it returns the smallest value lexicographically.
chars <- c("apple", "banana", "cherry") # Get the "minimum" value lexicographically min_char <- min(chars) print(min_char) # Outputs: "apple"
The min()
function in R is a fundamental tool when analyzing datasets. Whether identifying outliers or simply exploring data, knowing how to determine the smallest value is crucial. This function's versatility across different data structures, combined with its handling of missing values, ensures its prominent place in the R toolkit.
min() function in R:
min()
function in R is used to find the minimum value among a set of numeric values. It is applicable to vectors, matrices, and other numeric objects.# Using min() in R my_vector <- c(10, 25, 15, 30) minimum_value <- min(my_vector)
Finding minimum element in R:
min()
function to find the minimum element in a numeric vector in R.# Finding minimum element in R my_vector <- c(10, 25, 15, 30) minimum_value <- min(my_vector)
Getting the minimum value of a vector in R:
min()
function to obtain the minimum value of a numeric vector in R.# Getting the minimum value of a vector in R my_vector <- c(10, 25, 15, 30) minimum_value <- min(my_vector)
Using min() for element-wise comparison in R:
min()
function for element-wise comparison, determining the minimum value at each position in multiple vectors.# Using min() for element-wise comparison in R vector1 <- c(5, 10, 15) vector2 <- c(8, 12, 10) min_values <- min(vector1, vector2)
R min function with multiple vectors:
min()
function with multiple vectors, obtaining the minimum values at each corresponding position.# R min function with multiple vectors vector1 <- c(5, 10, 15) vector2 <- c(8, 12, 10) min_values <- min(vector1, vector2)
Finding minimum values in R matrices:
min()
function.# Finding minimum values in R matrices my_matrix <- matrix(c(10, 20, 30, 15, 25, 35), nrow = 2) min_values <- min(my_matrix)
Minimum element in a list in R:
min()
function to find the minimum element in a list in R.# Minimum element in a list in R my_list <- list(10, 25, 15, 30) min_value <- min(unlist(my_list))
Conditional minimum with min() in R:
min()
function with conditions to find the minimum value satisfying a specific criterion.# Conditional minimum with min() in R my_vector <- c(10, 25, 15, 30) condition <- my_vector > 20 conditional_min <- min(my_vector[condition])