R Programming: Find Maximum and Minimum values in a Vector
Write a R program to find the maximum and the minimum value of a given vector.
Sample Solution :
R Programming Code :
# Create a vector of numbers
nums = c(10, 20, 30, 40, 50, 60)
# Print a message indicating that the following output is the original vector
print('Original vector:')
# Print the content of the vector
print(nums)
# Print the maximum value of the vector, with a descriptive message
print(paste("Maximum value of the said vector:", max(nums)))
# Print the minimum value of the vector, with a descriptive message
print(paste("Minimum value of the said vector:", min(nums)))
Output:
[1] "Original vector:" [1] 10 20 30 40 50 60 [1] "Maximum value of the said vector: 60" [1] "Minimum value of the said vector: 10"
Explanation:
- Creates a vector nums: Initializes a vector named nums containing the values 10, 20, 30, 40, 50, 60.
- Prints the original vector: Outputs the text 'Original vector:' followed by the content of the nums vector.
- Finds the maximum value: Uses the max() function to determine the highest value in the nums vector and prints it along with the text "Maximum value of the said vector:".
- Finds the minimum value: Uses the min() function to determine the lowest value in the nums vector and prints it along with the text "Minimum value of the said vector:".
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to find the factors of a given number.
Next: Write a R program to get the unique elements of a given string and unique numbers of vector.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics