w3resource

R Programming: Combines two given vectors by columns, 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 :

v1 = c(1,3,5,7,9)
v2 = c(2,4,6,8,10)
print("Original vectors:")
print(v1)
print(v2)
print("Combines the said two vectors by columns:")
result = cbind(v1,v2)
print(result)
print("Combines the said two vectors by rows:")
result = rbind(v1,v2)
print(result)

Sample 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                         

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?



Follow us on Facebook and Twitter for latest update.