Write a R program to merge two given lists into one list

1. Introduction

This R program merges two given lists into a single list. The resulting list combines the elements of both input lists.

2. Program Steps

1. Create the first list.

2. Create the second list.

3. Merge the two lists into one.

4. Print the merged list.

3. Code Program

# Step 1: Create the first list
list1 <- list(a = 1, b = 2, c = 3)

# Step 2: Create the second list
list2 <- list(d = 4, e = 5, f = 6)

# Step 3: Merge the two lists into one
merged_list <- c(list1, list2)

# Step 4: Print the merged list
print("Merged List:")
print(merged_list)

Output:

Merged List:
$a
[1] 1
$b
[1] 2
$c
[1] 3
$d
[1] 4
$e
[1] 5
$f
[1] 6

Explanation:

1. list(a = 1, b = 2, c = 3): Creates the first list with elements a, b, and c.

2. list(d = 4, e = 5, f = 6): Creates the second list with elements d, e, and f.

3. c(list1, list2): Merges the first and second lists into a single list.

4. print("Merged List:"): Prints a message indicating the merged list.

5. print(merged_list): Displays the content of the merged list.


Comments