merge.list: Merge Two Lists

Description Usage Arguments Details Value Author(s) Examples

Description

merge.list merges two lists. If there are identical names in both lists, only the elements of the first list are considered.

Usage

1
2
## S3 method for class 'list'
merge(x, y = NULL, mergeUnnamed = TRUE, ...)

Arguments

x

a list of possibly named elements. All of these are in the merged list.

y

a list of possibly named elements or any object, which can be coerced to list. If an element has a name occuring also in the argument x, it will not be included in the merged list to avoid duplicate names. If NULL, x is returned.

mergeUnnamed

logical. If TRUE (the default) unnamed elements in the second list are always included.

...

arguments to be passed to or from methods.

Details

The purpose of this function is to merge two lists (e.g. argument lists). If a named element is found as well in the first list as in the second, only the value of the element in the first list is considered. One can think of the second list as a list of default values, which should be considered only if they are not set explicitly in the first list.

Unnamed elements in y are included in the merged list only if mergeUnnamed is TRUE.

Value

a list containing all elements of the argument x and those of y having names not occuring in x.

Author(s)

Thorn Thaler

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
merge(list(a=1, b="test"), list(3, b=2)) # list(a=1, b="test", 3)
merge(list(1), "test")                   # list(1, "test")
merge(list(1), "test", FALSE)            # list(1)
merge(list(1))                           # list(1)
merge(list(1, a=2, b=3), list(2, b=4))   # list(1, a=2, b=3, 2)
merge(list(1), list(2, b=3), FALSE)      # list(1, b=3)

a <- list(1, 2, 3)
b <- list("a", "b", "c")
names(a)[2] <- names(b)[2] <- "z"
all.equal(merge(a, b), list(1, z=2, 3, "a", "c")) # TRUE

Example output

$a
[1] 1

$b
[1] "test"

[[3]]
[1] 3

[[1]]
[1] 1

[[2]]
[1] "test"

[[1]]
[1] 1

[[1]]
[1] 1

[[1]]
[1] 1

$a
[1] 2

$b
[1] 3

[[4]]
[1] 2

[[1]]
[1] 1

$b
[1] 3

[1] TRUE

ttutils documentation built on May 1, 2019, 11:32 p.m.

Related to merge.list in ttutils...