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 :

# Create the first list containing numbers
n1 = list(1,2,3)

# Create the second list containing numbers
c1 = list(4,5,6)

# Print the message indicating the start of the original lists
print("Original lists:")

# Print the first list
print(n1)

# Print the second list
print(c1)

# Print the message indicating the start of conversion to vectors
print("Convert the lists to vectors:")

# Convert the first list to a vector
v1 = unlist(n1)

# Convert the second list to a vector
v2 = unlist(c1)

# Print the vector created from the first list
print(v1)

# Print the vector created from the second list
print(v2)

# Print the message indicating the start of adding two vectors
print("Add two vectors:")

# Add the two vectors element-wise
v = v1 + v2

# Print the resulting vector after addition
print("New vector:")

# Print the new vector
print(v)

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                         

Explanation:

  • Create Lists:
    • n1 = list(1,2,3) creates a list n1 containing the numbers 1, 2, and 3.
    • c1 = list(4,5,6) creates a list c1 containing the numbers 4, 5, and 6.
  • Print Original Lists:
    • print("Original lists:") prints a message indicating that the original lists will be displayed.
    • print(n1) prints the contents of the list n1.
    • print(c1) prints the contents of the list c1.
  • Convert Lists to Vectors:
    • print("Convert the lists to vectors:") prints a message indicating that the conversion process will begin.
    • v1 = unlist(n1) converts the list n1 into a vector v1.
    • v2 = unlist(c1) converts the list c1 into a vector v2.
  • Print Vectors:
    • print(v1) prints the vector v1 created from n1.
    • print(v2) prints the vector v2 created from c1.
  • Add Two Vectors:
    • print("Add two vectors:") prints a message indicating that the addition of vectors will be shown.
    • v = v1 + v2 performs element-wise addition of vectors v1 and v2, resulting in a new vector v.
  • Print New Vector:
    • print("New vector:") prints a message indicating that the new vector will be displayed.
    • print(v) prints the result of the vector addition.

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?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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/list/r-programming-list-exercise-9.php