How can I append values to a list in R?

Appending values to a list in R can be done in multiple ways. One way is by using the c() function, where the new value is added to the end of the list. Another method is by using the list() function, where the new value is added as a new element to the list. The append() function can also be used to add a new value to the list, either at the end or at a specified position. Finally, the [[]] operator can be used to add a new item to the list with a specific name and value. These methods allow for efficient and flexible ways to add values to a list in R.

Append Values to List in R (With Examples)


You can use the following syntax to append a single value to a list in R:

#get length of list called my_list
len <- length(my_list)

#append value of 12 to end of list
my_list[[len+1]] <- 12

And you can use the following syntax to append multiple values to a list in R:

#get length of list called my_list
len <- length(my_list)

#define values to append to list
new <- c(3, 5, 12, 14)

#append values to list
i = 1
while(i <= length(new)) {
    my_list[[i+len]] <- new[i]
    i <- i + 1
}

The following examples show how to use each of these functions in practice.

Example 1: Append a Single Value to a List

Suppose we have the following list in R:

#create list
my_list <- list(7, 14, c(1, 2, 3))

#view list
my_list

[[1]]
[1] 7

[[2]]
[1] 14

[[3]]
[1] 1 2 3

We can use the following syntax to append the value 12 to the end of the list:

#get length of list
len <- length(my_list)

#append value to end of list
my_list[[len+1]] <- 12

#view list
my_list

[[1]]
[1] 7

[[2]]
[1] 14

[[3]]
[1] 1 2 3

[[4]]
[1] 12

Example 2: Append Multiple Values to a List

Suppose we have the following list in R:

#create list
my_list <- list(7, 14, c(1, 2, 3))

#view list
my_list

[[1]]
[1] 7

[[2]]
[1] 14

[[3]]
[1] 1 2 3

We can use the following syntax to append several values to the end of the list:

#get length of list
len <- length(my_list)

#define values to append to list
new <- c(3, 5, 12, 14)

#append values to list
i = 1
while(i <= length(new)) {
    my_list[[i+len]] <- new[i]
    i <- i + 1
}

#display updated list
my_list

[[1]]
[1] 7

[[2]]
[1] 14

[[3]]
[1] 1 2 3

[[4]]
[1] 3

[[5]]
[1] 5

[[6]]
[1] 12

[[7]]
[1] 14

Cite this article

stats writer (2024). How can I append values to a list in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-append-values-to-a-list-in-r-examples-1-using-the-c-function-my_list-cmy_list-new_value2-using-the-list-function-my_list-listmy_list-new_value3-usi/

stats writer. "How can I append values to a list in R?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-append-values-to-a-list-in-r-examples-1-using-the-c-function-my_list-cmy_list-new_value2-using-the-list-function-my_list-listmy_list-new_value3-usi/.

stats writer. "How can I append values to a list in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-append-values-to-a-list-in-r-examples-1-using-the-c-function-my_list-cmy_list-new_value2-using-the-list-function-my_list-listmy_list-new_value3-usi/.

stats writer (2024) 'How can I append values to a list in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-append-values-to-a-list-in-r-examples-1-using-the-c-function-my_list-cmy_list-new_value2-using-the-list-function-my_list-listmy_list-new_value3-usi/.

[1] stats writer, "How can I append values to a list in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.

stats writer. How can I append values to a list in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top