w3resource

R Programming: Convert a given list to vector

R Programming: List Exercise-9 with Solution

Write a R program to convert a given list to vector.

Sample Solution :

R Programming Code :

n1 = list(1,2,3)
c1 = list(4,5,6)
print("Original lists:")
print(n1)
print(c1)
print("Convert the lists to vectors:")
v1 = unlist(n1)
v2 = unlist(c1)
print(v1)
print(v2)
print("Add two vectors:")
v = v1 + v2
print("New vector:")
print(v)

Sample Output:

[1] "Original lists:"
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[1]]
[1] 4

[[2]]
[1] 5

[[3]]
[1] 6

[1] "Convert the lists to vectors:"
[1] 1 2 3
[1] 4 5 6
[1] "Add two vectors:"
[1] "New vector:"
[1] 5 7 9                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to merge two given lists into one list.
Next: Write a R program to create a list of dataframes and access each of those data frames from the list.

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.