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 and Maximum element of a Vector - range() Function in R

The range() function in R provides a way to simultaneously get the minimum and maximum values from a numeric object, primarily from vectors. It's a convenient function when you want a quick look at the spread or range of your data.

In this tutorial, we'll delve into the usage of the range() function with vectors in R.

1. Basic Usage with Vectors

The range() function will return a vector with two elements: the minimum and the maximum.

vec <- c(5, 3, 9, 2, 7)

# Get the range
vec_range <- range(vec)
print(vec_range)  # Outputs: 2 9

2. Using range() with Missing Values (NA)

By default, if your vector contains missing values (NA), the range() function will return NA. To omit the missing values and get the range of the available numbers, use the na.rm argument.

vec_with_na <- c(5, 3, NA, 9, 2, 7)

# Get the range ignoring NA
vec_range <- range(vec_with_na, na.rm = TRUE)
print(vec_range)  # Outputs: 2 9

3. Using range() with Matrices

When applied to matrices, the range() function will consider all the elements and return the minimum and maximum values.

mat <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2)

# Get the range
mat_range <- range(mat)
print(mat_range)  # Outputs: 1 6

4. Using range() with Data Frames

With data frames, range() will consider all columns and give the overall minimum and maximum values. If you're interested in the range of a specific column, you can reference it.

df <- data.frame(A = c(1, 3, 5), B = c(4, 2, 6))

# Get the range across the entire data frame
df_range_all <- range(df, na.rm = TRUE)
print(df_range_all)  # Outputs: 1 6

# Get the range for column A
df_range_A <- range(df$A, na.rm = TRUE)
print(df_range_A)  # Outputs: 1 5

5. Using range() with Character Vectors

While its primary use is with numeric data, you can also use range() with character vectors, in which case it returns the minimum and maximum values lexicographically.

chars <- c("apple", "banana", "cherry")

# Get the lexicographical range
char_range <- range(chars)
print(char_range)  # Outputs: "apple" "cherry"

Conclusion

The range() function in R offers a straightforward way to identify both the smallest and largest values in your data set simultaneously. Its ability to handle different data structures and account for missing values makes it a handy tool in your R data analysis toolkit.

  1. range() function in R:

    • Description: The range() function in R is used to find the minimum and maximum values of a numeric vector. It returns a numeric vector with two elements, representing the range.
    • Code:
      # Using range() in R
      my_vector <- c(10, 25, 15, 30)
      value_range <- range(my_vector)
      
  2. Finding minimum and maximum in R vector:

    • Description: Demonstrates how to use the range() function to find the minimum and maximum values in a numeric vector in R.
    • Code:
      # Finding minimum and maximum in R vector
      my_vector <- c(10, 25, 15, 30)
      value_range <- range(my_vector)
      
  3. Getting the range of values in R:

    • Description: Illustrates using the range() function to obtain the range of values in a numeric vector in R.
    • Code:
      # Getting the range of values in R
      my_vector <- c(10, 25, 15, 30)
      value_range <- range(my_vector)
      
  4. Using range() for vector statistics in R:

    • Description: Explores the application of the range() function for obtaining basic statistics on a numeric vector in R.
    • Code:
      # Using range() for vector statistics in R
      my_vector <- c(10, 25, 15, 30)
      vector_stats <- range(my_vector)
      
  5. R range function with multiple vectors:

    • Description: Demonstrates using the range() function with multiple vectors, obtaining the range for each vector.
    • Code:
      # R range function with multiple vectors
      vector1 <- c(5, 10, 15)
      vector2 <- c(8, 12, 10)
      range_values <- range(vector1, vector2)
      
  6. Finding range in R matrices:

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

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

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