2 Ways to Append an Element to a List in R

Adding elements to a list is a fairly common task in R, so the language provides methods and operators to help you with this operation. However, before deep diving into appending to a list, let’s see what a list is and how to create it.

What is a List in R?

The list is a data structure having elements of different data types. To access the list, use indexing. Use integer, logical, or character vectors for indexing.

How to create a list?

To create a list in R, use the list() function. Then, the list can be modified, added, updated, or deleting the list elements. In addition, you can append a single element or multiple elements to a list even after creating a list. Let’s see how to do it in action.

R append to list

2 Easy ways to append an element to a list in R.

  1. Using the append(): The append() function appends the element at the end of the list.
  2. Using the c(): The c() function concatenates the element to the list.

Method 1: Use the append() method

To append an element to a List in R, use the append() function. The append()  is a built-in function that accepts three parameters input list, a string or list you wanted to append, and position. Then, the element is appended at the end of the list in the specified order.

The append() function also allows us to define where to append the values within the list or vector. This will add the elements after the named element.

You can use the concatenate approach to add components to a list. While concatenate does a great job of adding elements to the R list, the append() function operates faster.

Syntax

append(data, values, after = length(data))

Parameters

data: It is a list or vector.

values: It is a vector or list to be included in the modified vector.

after: It is a subscript, after which the values are to be appended.

R code to append an element to a list

list1 <- list(a = 1, b = 2, c = 3)
list2 <- list(d = 4)
appended_list <- append(list1, list2)
print(appended_list)

Output

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

In this example, we appended a list element to the list using the append() element.

Appending an element at the specific position

To append an element at the specific position, use the “after” parameter of the append() function. Using the after parameter, you can specify the position where you want to add the element.

lt <- list("SBF", "Alameda")
lt2 <- append(lt, "FTX", after = 1)
print(lt2)

Output

[[1]]
[1] "SBF"

[[2]]
[1] "FTX"

[[3]]
[1] "Alameda"

In this example, we are telling the R compiler to add an element after the 1st element of the list. 

You can see from the output that it adds an element after the first element after the “SBF” element.

How to append multiple elements to list in R

To append multiple elements to a list in R, use the rlist package that provides a list.append() function. The list.append() is a “rlist” package function that accepts a list and multiple elements that need to be appended to the list.

library("rlist")

lt <- list("SBF", "Alameda")
lt2 <- list.append(lt, "FTX", "CZs")
print(lt2)

Output

[[1]]
[1] "SBF"

[[2]]
[1] "Alameda"

[[3]]
[1] "FTX"

[[4]]
[1] "CZs"

You can see that an existing list contains only two elements but using the list.append() function, we appended two more elements, and finally, we got the list with four elements.

Method 2: Use the c() function

The c() function to append values to a list. It is a straightforward approach to append elements to the list.

my_list <- list(1, 2, 3)

my_list <- c(my_list, 4)

print(my_list)

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

Appending an element to a vector

To append an element to a vector in R, use the append() function.

rv1 <- c(10, 20, 30, 40)
rv2 <- append(rv1, 50, after = 40)
print(rv2)

Output

Rscript Pro.R
[1] 10 20 30 40 50

In this example, we are appending an item after the specific component. In our case, it is 40, and it is last.

FAQ

How to add an element to a specific position in a list in R?

Use the “[] operator” to insert the new element to a specific position in a list in R. For example, if you have a list called main_list and you want to add the element x at the second position, use this code: main_list[2] <- x.

How to add multiple elements to a list in R?

Use the append() function multiple times to append multiple elements to a list in R.

How to add an element to a nested list in R?

Use the [] operator to access the sub-list and then use the c() or append() function to add the new element to a nested list in R. For example, if you have a nested list called main_list with a sub-list at position 2 and you want to add the element x, use this code: main_list[[2]] <- c(main_list[[2]], x).

Conclusion

To append a single element to a list in R, use the append() function.

To append multiple elements to a list in R, use the rlist package’s list.append() function.

That is it.

Leave a Comment