R Programming: Combine two given Vectors by Columns and Rows
R Programming: Vector Exercise-25 with Solution
Write a R program to combines two given vectors by columns, rows.
Sample Solution :
R Programming Code :
# Create the first vector 'v1' with the elements 1, 3, 5, 7, and 9
v1 = c(1, 3, 5, 7, 9)
# Create the second vector 'v2' with the elements 2, 4, 6, 8, and 10
v2 = c(2, 4, 6, 8, 10)
# Print a message indicating the original vectors
print("Original vectors:")
# Print the first vector 'v1'
print(v1)
# Print the second vector 'v2'
print(v2)
# Print a message indicating the result of combining vectors by columns
print("Combines the said two vectors by columns:")
# Combine 'v1' and 'v2' by columns and store the result in 'result'
result = cbind(v1, v2)
# Print the combined result of vectors 'v1' and 'v2' by columns
print(result)
# Print a message indicating the result of combining vectors by rows
print("Combines the said two vectors by rows:")
# Combine 'v1' and 'v2' by rows and store the result in 'result'
result = rbind(v1, v2)
# Print the combined result of vectors 'v1' and 'v2' by rows
print(result)
Output:
[1] "Original vectors:" [1] 1 3 5 7 9 [1] 2 4 6 8 10 [1] "Combines the said two vectors by columns:" v1 v2 [1,] 1 2 [2,] 3 4 [3,] 5 6 [4,] 7 8 [5,] 9 10 [1] "Combines the said two vectors by rows:" [,1] [,2] [,3] [,4] [,5] v1 1 3 5 7 9 v2 2 4 6 8 10
Explanation:
- Create the first vector:
- v1 = c(1, 3, 5, 7, 9)
Creates a vector v1 containing the elements 1, 3, 5, 7, and 9. - Create the second vector:
- v2 = c(2, 4, 6, 8, 10)
Creates a vector v2 containing the elements 2, 4, 6, 8, and 10. - Print message (Original vectors):
- print("Original vectors:")
Prints a message indicating that the following outputs will display the original vectors. - Display the first vector:
- print(v1)
Displays the content of the first vector v1. - Display the second vector:
- print(v2)
Displays the content of the second vector v2. - Print message (Combine by columns):
- print("Combines the said two vectors by columns:")
Prints a message indicating that the following output will show the vectors combined by columns. - Combine vectors by columns:
- result = cbind(v1, v2)
Combines v1 and v2 by columns, creating a matrix where v1 and v2 are the columns. - Display combined vectors by columns:
- print(result)
Displays the matrix created by combining v1 and v2 by columns. - Print message (Combine by rows):
- print("Combines the said two vectors by rows:")
Prints a message indicating that the following output will show the vectors combined by rows. - Combine vectors by rows:
- result = rbind(v1, v2)
Combines v1 and v2 by rows, creating a matrix where v1 and v2 are the rows. - Display combined vectors by rows:
- print(result)
Displays the matrix created by combining v1 and v2 by rows.
R Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a R program to create a vector and find the length and the dimension of the vector.
Next: Write a R program to test whether the value of the element of a given vector greater than 10 or not. Return TRUE or FALSE.
Test your Programming skills with w3resource's quiz.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/r-programming-exercises/vector/r-programming-vector-exercise-25.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics