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 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.
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
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
range()
with MatricesWhen 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
range()
with Data FramesWith 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
range()
with Character VectorsWhile 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"
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.
range() function in R:
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.# Using range() in R my_vector <- c(10, 25, 15, 30) value_range <- range(my_vector)
Finding minimum and maximum in R vector:
range()
function to find the minimum and maximum values in a numeric vector in R.# Finding minimum and maximum in R vector my_vector <- c(10, 25, 15, 30) value_range <- range(my_vector)
Getting the range of values in R:
range()
function to obtain the range of values in a numeric vector in R.# Getting the range of values in R my_vector <- c(10, 25, 15, 30) value_range <- range(my_vector)
Using range() for vector statistics in R:
range()
function for obtaining basic statistics on a numeric vector in R.# Using range() for vector statistics in R my_vector <- c(10, 25, 15, 30) vector_stats <- range(my_vector)
R range function with multiple vectors:
range()
function with multiple vectors, obtaining the range for each vector.# R range function with multiple vectors vector1 <- c(5, 10, 15) vector2 <- c(8, 12, 10) range_values <- range(vector1, vector2)
Finding range in R matrices:
range()
function.# Finding range in R matrices my_matrix <- matrix(c(10, 20, 30, 15, 25, 35), nrow = 2) matrix_range <- range(my_matrix)
Minimum and maximum element in a list in R:
range()
function to find the minimum and maximum elements in a list in R.# Minimum and maximum element in a list in R my_list <- list(10, 25, 15, 30) list_range <- range(unlist(my_list))
Conditional range with range() in R:
range()
function with conditions to find the range of values satisfying a specific criterion.# Conditional range with range() in R my_vector <- c(10, 25, 15, 30) condition <- my_vector > 20 conditional_range <- range(my_vector[condition])