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

Get the Minimum element of an Object - min() Function in 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.

1. Basic Usage with Vectors

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

2. Using 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

3. Using min() with Matrices

For 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

4. Using min() with Data Frames

When 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

5. Comparing Multiple Values Directly

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

6. Using min() with Character Vectors

While 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"

Conclusion

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.

  1. min() function in R:

    • Description: The 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.
    • Code:
      # Using min() in R
      my_vector <- c(10, 25, 15, 30)
      minimum_value <- min(my_vector)
      
  2. Finding minimum element in R:

    • Description: Demonstrates how to use the min() function to find the minimum element in a numeric vector in R.
    • Code:
      # Finding minimum element in R
      my_vector <- c(10, 25, 15, 30)
      minimum_value <- min(my_vector)
      
  3. Getting the minimum value of a vector in R:

    • Description: Illustrates using the min() function to obtain the minimum value of a numeric vector in R.
    • Code:
      # Getting the minimum value of a vector in R
      my_vector <- c(10, 25, 15, 30)
      minimum_value <- min(my_vector)
      
  4. Using min() for element-wise comparison in R:

    • Description: Explores the use of the min() function for element-wise comparison, determining the minimum value at each position in multiple vectors.
    • Code:
      # Using min() for element-wise comparison in R
      vector1 <- c(5, 10, 15)
      vector2 <- c(8, 12, 10)
      min_values <- min(vector1, vector2)
      
  5. R min function with multiple vectors:

    • Description: Demonstrates using the min() function with multiple vectors, obtaining the minimum values at each corresponding position.
    • Code:
      # R min function with multiple vectors
      vector1 <- c(5, 10, 15)
      vector2 <- c(8, 12, 10)
      min_values <- min(vector1, vector2)
      
  6. Finding minimum values in R matrices:

    • Description: Illustrates how to find the minimum values in R matrices using the min() function.
    • Code:
      # Finding minimum values in R matrices
      my_matrix <- matrix(c(10, 20, 30, 15, 25, 35), nrow = 2)
      min_values <- min(my_matrix)
      
  7. Minimum element in a list in R:

    • Description: Highlights using the min() function to find the minimum element in a list in R.
    • Code:
      # Minimum element in a list in R
      my_list <- list(10, 25, 15, 30)
      min_value <- min(unlist(my_list))
      
  8. Conditional minimum with min() in R:

    • Description: Explores using the min() function with conditions to find the minimum value satisfying a specific criterion.
    • Code:
      # Conditional minimum with min() in R
      my_vector <- c(10, 25, 15, 30)
      condition <- my_vector > 20
      conditional_min <- min(my_vector[condition])