merge.list: Merge Two Lists

Description Usage Arguments Examples

View source: R/utils.R

Description

Merges two lists. Every named element in the second argument is added recursively at the corresponding position in the first argument by name, over-writing existing values as necessary. Every un-named argument is added if there is no named argument to over-write.

Usage

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

Arguments

x

a list (coerced if not)

y

a list (coerced if not)

...

ignored

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
foo <- list(
  a = list(         # substituted by name
    col = 'red',    # substituted by name
    lty = 'dashed', # substituted by name
    alpha = 1,      # preserved
    8,              # preserved, since element 4 in replacement matches by name
    9               # substituted by position
  ),
  letters[8:10],    # preserved, siince elment 2 in replacement matches by name
  b = 3
)

bar <- list(
  letters[11:13],  # ignored: position conflict with named element
  b = 2,           # substituted by name
  a = list(        # substituted by name
    'blue',        # ignored: position conflict with named element
    col = 'green', # substituted by name
    lty = 'solid', # substituted by name
    lwd = 2,       # added by name
    3,             # substituted by postion
    4,             # added by postion
    hue = 5        # added by name
  ),
  'baz'            # added by postion
)

foo
bar
merge(foo,bar)
merge(list(1), list(2,foo = 3)) # 3 is assigned and named
merge(list(1), list(foo = 2,3)) # 3 is ignored since position 2 has been named by time of evaluation
merge(list(foo = 1), list(2,foo = 3)) # 2 ignored since pos. matches named argument; 3 overwrites
merge(list(foo = 1), list(2,3)) # 2 is ignored since position matches a named argument; 3 added

Example output

$a
$a$col
[1] "red"

$a$lty
[1] "dashed"

$a$alpha
[1] 1

$a[[4]]
[1] 8

$a[[5]]
[1] 9


[[2]]
[1] "h" "i" "j"

$b
[1] 3

[[1]]
[1] "k" "l" "m"

$b
[1] 2

$a
$a[[1]]
[1] "blue"

$a$col
[1] "green"

$a$lty
[1] "solid"

$a$lwd
[1] 2

$a[[5]]
[1] 3

$a[[6]]
[1] 4

$a$hue
[1] 5


[[4]]
[1] "baz"

$a
$a$col
[1] "green"

$a$lty
[1] "solid"

$a$alpha
[1] 1

$a[[4]]
[1] 8

$a[[5]]
[1] 3

$a$lwd
[1] 2

$a$hue
[1] 5


[[2]]
[1] "h" "i" "j"

$b
[1] 2

[[4]]
[1] "baz"

[[1]]
[1] 2

$foo
[1] 3

[[1]]
[1] 1

$foo
[1] 2

$foo
[1] 3

$foo
[1] 1

[[2]]
[1] 3

metaplot documentation built on May 1, 2019, 10:17 p.m.