Introduction
Finding the minimum and maximum values in a dataset is a fundamental task in data analysis. In R, you can easily determine these values using built-in functions. This guide will walk you through writing an R program that finds and displays the minimum and maximum values in a numeric vector.
Problem Statement
Create an R program that:
- Creates a numeric vector with a series of values.
- Finds the minimum value in the vector.
- Finds the maximum value in the vector.
- Displays both the minimum and maximum values.
Example:
- Input: A vector with elements
c(10, 25, 3, 47, 15)
- Output: Minimum value:
3
, Maximum value:47
Solution Steps
- Create a Numeric Vector: Use the
c()
function to create a vector with a sequence of numeric values. - Find the Minimum Value: Use the
min()
function to find the minimum value in the vector. - Find the Maximum Value: Use the
max()
function to find the maximum value in the vector. - Display the Minimum and Maximum Values: Use the
print()
function to display the minimum and maximum values.
R Program
# R Program to Find Minimum and Maximum Values # Author: Ramesh Fadatare # Step 1: Create a numeric vector with a series of values my_vector <- c(10, 25, 3, 47, 15) # Step 2: Find the minimum value in the vector min_value <- min(my_vector) # Step 3: Find the maximum value in the vector max_value <- max(my_vector) # Step 4: Display the minimum and maximum values print(paste("Minimum value:", min_value)) print(paste("Maximum value:", max_value))
Explanation
Step 1: Create a Numeric Vector
- The
c()
function is used to create a numeric vector with the elementsc(10, 25, 3, 47, 15)
.
Step 2: Find the Minimum Value
- The
min()
function is used to find the minimum value in the vector, which is stored in themin_value
variable.
Step 3: Find the Maximum Value
- The
max()
function is used to find the maximum value in the vector, which is stored in themax_value
variable.
Step 4: Display the Minimum and Maximum Values
- The
print()
function is used to display the minimum and maximum values with descriptive messages.
Output Example
Example:
[1] "Minimum value: 3" [1] "Maximum value: 47"
Conclusion
This R program demonstrates how to find the minimum and maximum values in a numeric vector using the min()
and max()
functions. It covers basic operations such as vector creation, finding extreme values, and displaying results, making it a valuable example for beginners learning R programming. Understanding how to identify these values is crucial for data analysis and manipulation.