4 Ways to Append Multiple Elements to a List in R

Building a list of multiple elements over time is helpful. If you need to create a list of names, emails, and phone numbers and then append all of these lists together to create a single list, you require some methods or approaches to do that.

How to Append Multiple Elements to a List in R

4 easy ways to append multiple elements to a list in R.

  1. Using c() function
  2. Using append() function
  3. Using insert() function
  4. Using list.append() function

Approach 1: Using the c() function

The c() function combines multiple elements into a single list.

To append multiple elements to a list in R using the c() function, follow these steps.

  1. Create a list using the list() function
  2. Use the c() function to append multiple elements to the list.
  3. Print the list using the print() function.

Step 1: Create a list

list <- list(11, 19)

Step 2: Use the c() function

Using the c() function, we will add multiple elements to the list.

list <- list(11, 19)

list_2 <- c(list, 21, 46)

Step 3: Print the list

To print the list in R, use the print() function.

list <- list(11, 19)

list_2 <- c(list, 21, 46)

print(list_2)

Output

[[1]]
[1] 11

[[2]]
[1] 19

[[3]]
[1] 21

[[4]]
[1] 46

Approach 2: Using the append() function

Follow the below steps to append multiple elements to a list in R.

  1. Create two lists using the list() function
  2. Use the append() function to append multiple elements to the list.
  3. Print the list using the print() function.

Step 1: Create two lists

list_1 <- list(11, 19)
list_2 <- list(21, 46)

Step 2: Use the append() function

The append() function lets you add multiple elements to the list.

list_1 <- list(11, 19)

list_2 <- list(21, 46)

final_list <- append(list_1, list_2)

In this code, we defined two different lists, and using the append() function, we added multiple elements together.

Step 3: Print the list with appended elements

To print the list in R, use the print() function.

list_1 <- list(11, 19)

list_2 <- list(21, 46)

final_list <- append(list_1, list_2)

print(final_list)

Output

[[1]]
[1] 11

[[2]]
[1] 19

[[3]]
[1] 21

[[4]]
[1] 46

Approach 3: Using the insert() function

Follow the below steps to append multiple elements to a list using the insert() function in R.

  1. Create a list using the list() function
  2. Use the rlist module’s list.insert() function to append multiple elements to the list.
  3. Print the list using the print() function.

Step 1: Create a list

list_1 <- list(11, 19)

Step 2: Use the insert() function

To insert an element at a specific position in a list in R, use the list.insert() function.

library("rlist")

list_1 <- list(11, 19)

list_1 <- list.insert(list_1, 3, 21, 36)

Step 3: Print the list

Use the print() function to print the list.

library("rlist")

list_1 <- list(11, 19)

list_1 <- list.insert(list_1, 3, 21, 36)

print(list_1)

Output

[[1]]
[1] 11

[[2]]
[1] 19

[[3]]
[1] 21

[[4]]
[1] 36

Approach 4: Using the list.append() function

Follow the below steps to append multiple elements to a list using the rlist’s list.append() function in R.

  1. Create a list using the list() function
  2. Use the rlist module’s list.append() function to append multiple elements to the list.
  3. Print the list using the print() function.

Step 1: Create a list.

library("rlist")

list_1 <- list(11, 19)

Step 2: Use the list.append() function.

The list.append() is the rlist module’s function that appends multiple elements to a list.

library("rlist")

list_1 <- list(11, 19)

list_1 <- list.append(list_1, 21, 36)

Step 3: Print the list.

Use the print() function to print the list.

library("rlist")

list_1 <- list(11, 19)

list_1 <- list.append(list_1, 21, 36)

print(list_1)

Output

[[1]]
[1] 11

[[2]]
[1] 19

[[3]]
[1] 21

[[4]]
[1] 36

Conclusion

Use either c() or append() function to append multiple elements to the list in R.

To append multiple elements at a specific position in R, use the rlist module’s list.insert() function.

Related posts

Append a Single Element to a List in R

Append multiple elements to a Vector in R

Leave a Comment