How to Append an Element to a List in R [4 Ways]

Here are four ways to add/append an element to a list in R:

  1. Using append()
  2. Using c()
  3. Using the ‘rlist’ library
  4. Using $ operator

Method 1: Using append()

You can use the “append()” function to add an element at the end of the list, but it provides an argument through which you can add an element anywhere.

Syntax

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

Parameters

  1. data: It is a list.
  2. values: It is a vector or list to be included in the modified vector.
  3. after: It is a subscript, after which the values are to be appended.

Example 1: Adding an element at the end of the list

Visual representation of appending an element to the list using append() function in R

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

Example 2: Appending an element at the specific position

Visual representation of appending an element at specific position in the list

To append an element at a specific position, use the “after” parameter.

list1 <- list(1, 2, 3)

list2 <- append(list1, 4, after = 1)

print(list2)

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 4

[[4]]
[1] 3

Example 3: Appending multiple elements

Visual representation of adding multiple elements to the list

list1 <- list(1, 2, 3)
elements <- c(4, 5, 6)

multi_list <- append(list1, elements)

print(multi_list)

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

Method 2: Using c()

Visual Representation of using c() function to append an element to the list

 

To append single or multiple elements to a list simultaneously, use the c() function.

list1 <- list(1, 2, 3)

list1 <- c(list1, 4)

print(list1)

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

Method 3: Using the ‘rlist’ library

Visual representation of using rlist library to add a new element to the list

For this approach, you need to install the “rlist” library. Use the below command to install it.

install.packages("rlist")

Screenshot of installing rlist package

library("rlist")

list1 <- list(a = 1, b = 2, c = 3)

new_list <- list.append(list1, d = 4)

print(new_list)

Output

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

Method 4: Using $ operator

The $ operator is not typically used for adding new elements, it can be used for modification. However, if the name provided after the $ does not exist in the list, it will add a new element with that name.

Example 1: Adding a new element

Using $ operator to add a new element

list1 <- list(a = 1, b = 2, c = 3)

list1$d <- 4

list1

Output

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

This approach is still not suitable for adding new elements instead, you can use the $ operator to modify existing list elements.

Example 2: Modifying an existing element

Visual representation of Modifying an existing element of a list

list1 <- list(a = 1, b = 2, c = 3)

list1$b <- 21

cat("After modifying the list", "\n")

print(list1)

Output

After modifying the list

$a
[1] 1

$b
[1] 21

$c
[1] 3

That’s it!

Leave a Comment