Merge lists with different or intersecting names
Arguments
- x, y
Lists to merge
- keep
When matching names are found, from which object should the values be retained;
"x"retains values fromx,"y"retains values fromy.- null
Method for handling
NULLvalues. When two values are passed, they will be applied toxandyrespectively. When a single value is passed, it will be applied to bothxandy."ignore":NULLvalues are ignored. When passes tox, theNULLvalues will be retained if they are not overridden byy."drop":NULLvalues are dropped from merge and will not appear in the output."keep":NULLvalues are retained in the output and can override other values.
Examples
x <- list(a = 1, b = 2, c = NULL, d = NULL)
y <- list(a = 2, b = NULL, c = 3)
# compared to:
utils::modifyList(x, y)
#> $a
#> [1] 2
#>
#> $c
#> [1] 3
#>
#> $d
#> NULL
#>
utils::modifyList(x, y, keep.null = TRUE)
#> $a
#> [1] 2
#>
#> $b
#> NULL
#>
#> $c
#> [1] 3
#>
#> $d
#> NULL
#>
merge_list(x, y)
#> $a
#> [1] 1
#>
#> $b
#> [1] 2
#>
#> $c
#> [1] 3
#>
#> $d
#> NULL
#>
merge_list(x, y, keep = "y")
#> $a
#> [1] 2
#>
#> $b
#> [1] 2
#>
#> $c
#> [1] 3
#>
#> $d
#> NULL
#>
merge_list(x, y, null = "drop")
#> $a
#> [1] 1
#>
#> $b
#> [1] 2
#>
#> $c
#> [1] 3
#>