Append To List R: How To Add Elements To The List in R

append To List R

In this article, we will share with you how to append to list R with the append() function. You can add an element, multiple elements, or the list to the current list in R by the append() function. Let’s learn more about it with the explanation and examples below.

Append To List R

What does the append function do in R?

The append() function in R is used to append an element, multiple elements, or the list to the current list with the specified position. Let’s take a look at the syntax of the append() function.

Syntax

append(data, values, position = length(x))

Parameters

  • data: The new value to append to the list.
  • values: The current list or vector.
  • after: A subscript.

After learning the usage and the syntax of the append() function, you will learn how to append to list R by the append() function in the next title below.

How To Append To List R

You can add an element, multiple elements, and a list to the current list by the append() function.

Append an element to list

You can append an element to the list by the append() function.

Look at the example below.

# Create the list
current_list <- list("Argentina")

# Append 'France' to the current list
new_list <- append("France", current_list)
new_list

Output

[[1]]
[1] "France"

[[2]]
[1] "Argentina"

Append multiple elements to list

You can append multiple elements to the list by the append() function.

Look at the example below.

# Create the list
current_list <- list("Argentina", "France")

# Append 'Morocco' and 'Croatia' to the current list
new_list <- append(c("Morocco", "Croatia"), current_list)
new_list

Output

[[1]]
[1] "Morocco"

[[2]]
[1] "Croatia"

[[3]]
[1] "Argentina"

[[4]]
[1] "France"

Append a list to the current list

You can append a list to the current list in R by the append() function.

Look at the example below.

# Create the first list.
list1 <- list(1, 2, 3, 4)

# Create the second list.
list2 <- list(5, 6, 7, 8)

# Append list1 to list2.
new_list <- append(list1, list2)
new_list

Output

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

[[7]]
[1] 7

[[8]]
[1] 8

You can learn the series of R tutorials here.

Summary

We have shown you the usage, the syntax of the append() function, which is used to append to list R. You can add an element, multiple elements, or a list to the current list by the append() function. If you have any questions about this tutorial, leave your comment below, and I will answer your questions. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *