R Programming: Divide two Vectors of integers type and length 3
Write a R program to divide two vectors of integers type and length 3.
Sample Solution :
R Programming Code :
# Create a vector 'x' with three numeric elements
x = c(10, 20, 30)
# Create a vector 'y' with three numeric elements
y = c(20, 10, 40)
# Print a message to indicate the original vectors
print("Original Vectors:")
# Print the contents of vector 'x'
print(x)
# Print the contents of vector 'y'
print(y)
# Print a message to indicate the operation of dividing vectors
print("After dividing Vectors:")
# Divide each element of vector 'x' by the corresponding element in vector 'y' and store the result in 'z'
z = x / y
# Print the resulting vector 'z'
print(z)
Output:
[1] "Original Vectors:" [1] 10 20 30 [1] 20 10 40 [1] "After dividing Vectors:" [1] 0.50 2.00 0.75
Explanation:
- x = c(10, 20, 30): Creates a numeric vector x containing the elements 10, 20, and 30.
- y = c(20, 10, 40): Creates a numeric vector y containing the elements 20, 10, and 40.
- print("Original Vectors:"): Prints the message "Original Vectors:" to the console.
- print(x): Prints the contents of the vector x to the console.
- print(y): Prints the contents of the vector y to the console.
- print("After dividing Vectors:"): Prints the message "After dividing Vectors:" to the console.
- z = x / y: Divides each element of vector x by the corresponding element in vector y and stores the result in the vector z.
- print(z): Prints the resulting vector z to the console.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to multiply two vectors of integers type and length 3.
Next: Write a R program to find Sum, Mean and Product of a 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