R Basic

How to Append an Element to a List at Any Position in R

To grow the list, you can add an element (numeric value, character vectors, other lists, data frames, or functions) at whichever position you like. You can add a single element, multiple elements, or a list as an element. The final way depends on the user’s intention.

Here are five ways to append an element to a list:

  1. Using append()
  2. Using c()
  3. Using the rlist library
  4. Using $ operator
  5. Using the double bracket ( [[ ) (index assignment)

Method 1: Using append()

If you are picky about which position you want to add an element, append() is the method for you. It lets you add an element at the end of the list but provides an argument “after” through which you can add an element anywhere.

It is like a magic wand in your hand that lets you place your new element exactly where you want it in the list.

Adding an element at the end of the list

The append() method creates a new list instead of modifying in place. If you do not want to modify an original list, you can use this approach as this is a built-in function.

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

Appending an element at the specific position

Here, we added an element (4) after the position 1 using 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

Appending multiple elements

The above figure shows that we added 3 elements (4, 5, 6) at the end of 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

Time complexity: O(n), where n is the number of elements in the list.

Space complexity: O(n), as it creates a new list.

Method 2: Using c()

You can combine the original list with a new element using the c() function. It will help you add single or multiple elements to a list simultaneously. This approach is straightforward but not the most efficient for large lists due to R’s copy-on-modify semantics.

 

The above figure shows that we appended element 4 at the end of the position.

list1 <- list(1, 2, 3)

list1 <- c(list1, 4)

print(list1)

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

You can use this approach when you need fine-grained control over the insertion position.

If the new element is a list and you want it nested as a single item, wrap it in list().

Time complexity: O(n), where n is the number of elements in the list.

Space complexity: O(n), as it creates a new list.

Method 3: Using the ‘rlist’ library

The above figure shows that we used the rlist library’s list.append() function to add an element (4) at the end of the list highlighted by the red marker.

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

install.packages("rlist")

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 but 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.

Adding 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

Modifying an existing element

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

Time complexity: O(1) for appending to the end, O(n) for inserting in the middle.

Space complexity: O(1)

Method 5: Using the double bracket [[

The double bracket ([[) approach is more direct than the c() function and avoids some copying overhead, which is especially helpful in any type of loop.

list1 <- list(1, 2, 3)

list1[[length(list1) + 1]] <- 4

cat("After adding an element the list", "\n")

print(list1)

Output

After adding an element the list

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

The output shows that the element “4” is added at the end of the list.

Time complexity: O(1) for appending to the end, O(n) for inserting in the middle.

Space complexity: O(1)

Recent Posts

How to Remove NULL from List and Nested List in R

NULL represents a null object, and sometimes, it's logical for the project to filter it…

12 hours ago

How to Remove the Last Row or N Rows from DataFrame in R

In a real-life dataset, the last row may contain metadata, summaries, footnotes, or unwanted rows…

2 days ago

How to Remove the First Row of DataFrame in R

When we attempt to remove the first row of a data frame, we are essentially…

5 days ago

R basename() Function

The basename() is a base R function that extracts the last component (or the 'base…

7 days ago

How to Remove Duplicates from a Vector in R

Duplicate elements in a vector means those elements appear more than once. Duplicates can skew…

1 week ago

How to Remove Single or Multiple Rows from Data Frame in R

For meaningful and accurate analysis, you may want to remove rows with too many missing…

1 week ago