You are currently viewing Add or Append Element to List in R

To add or append an element to the list in R use the append() function. This function takes 3 parameters input list, the string or list you wanted to append, and position.

The list.append() function from the rlist package can also append one list with another in R. In this article, I will explain how to append/add an element to a list by using the R base append() function and list.append() function from the list package with multiple examples.

Related: You can also add an element to the list using a loop in r.

1. Quick Examples of Adding Elements to List

Following are quick examples of adding or appending elements to the list in R.


# Quick examples of appending element to list

# Example 1: Using R base append()
li = list('java','python')
li2 <- append(li,'r')
print(li2)

# Example 2: Add element to list at specified position
li = list('java','python')
li2 <- append(li,'r',after=1)
print(li2)

# Example 3: Using list.append()
library('rlist')
li2 <- list.append(li,'r')
print(li2)

# Example 4: Named List
library('rlist')
li = list(id=11,name='python')
li2 <- list.append(li,pages=34)
print(li2)

# Example 5: Append list with another list
li = list('java','python')
li2 = list('r','php')
li3 <- append(li,li2)
print(li3)

2. R Base append() Syntax

Following is the syntax of the R base append() function


# Syntax of append()
append(x, values, after = length(x))

Parameters

  • x – Input list
  • values – string, list or vector.
  • after – Position to add. Optional. When not specified the element is added at the end of the list.

3. Append Element to List

You can use the append() function to add an element to the existing list in R. If you do not specify the position(in which we want to specify the position of an element) by default, it adds an element at the end of the list. The following example adds an element r to the list.


# Add element to list
li = list('java','python')
print(li)
li2 <- append(li,'r')
print(li2)

Yields below output. Note that we have added the item r to the list.

r add element list

4. Add Element to List at Specified Position

You can use after parameter to specify the position where you want to add the element. The following example adds elements after the first position.


# Add element to list at specified position
li = list('java','python')
li2 <- append(li,'r',after=1)
print(li2)

Yields below output.

r add element list

5. Add Multiple Elements to List

Package rlist provides a list.append() function to add multiple elements to the list in R. In order to use this function first, you need to install R package by using install.packages("rlist") and load it using the library("rlist").


# Add multiple elements to list
library('rlist')
li2 <- list.append(li,'r','50')
print(li2)

Yields below output.


# Output
[[1]]
[1] "java"

[[2]]
[1] "python"

[[3]]
[1] "r"

[[4]]
[1] "50"

Using this function we can also append elements to the named list.


# Named List
library('rlist')
li = list(id=11,name='python')
li2 <- list.append(li,pages=34)
print(li2)

6. Append the List with Another List

To append one list with another list pass both lists to the append() function, this adds a second list to the end of the first list and returns a single list.


# Append list with another list
li = list('java','python')
li2 = list('r','php')
li3 <- append(li,li2)
print(li3)

# Output:
# [[1]]
# [1] "java"

# [[2]]
# [1] "python"

# [[3]]
# [1] "r"

# [[4]]
# [1] "php"

7. Using c() to Append List

If you are unaware that c() is a combined function used to merge the list, vector etc.. The following example uses c() to append two lists.


# using c()
li3 = c(li,li2)
print(li3)

Yields the same as the above output.

Frequently Asked Questions of Append Element to List in R

How do I append an element to a list in R?

You can use the c() function to concatenate or append elements to a list. For example, append_list <- c(my_list1, new_element)

How can I append multiple elements at once?

rlist package provides a list.append() function to append multiple elements to the list in R. For example, library('rlist')
my_list <- list.append(my_list1,'new_element1','new_element2')

How do you insert an element at a specific position in the list?

Using the R base append() function with the after parameter to insert an element at a specific position. For example, my_list <- append(my_list1, new_element, after = 2)

How can I append another list to an existing list?

To append a list to an existing list you can use the R base function. It appends the new list at the end of the existing list. For example, my_list <- append(my_list1, my_list2)

Conclusion

In this article, you have learned how to add or append an element to the list by using R base append(), list.append() from the rlist package. You can find the complete example from this article at Github R Programming Examples Project.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium