Ordinary base lists and vectors do not retain their attributes when subsetted or appended. This function appends items to a list while preserving the attributes of items in the list (but not of the list itself).
append_attr(x, y)
# S4 method for list,list
append_attr(x, y)
A list
of items with optional attributes.
An updated list
with attributes.
Similar to updateList
but does not require named lists.
library(igraph) # igraph exports magrittr's pipe operator
#>
#> Attaching package: ‘igraph’
#> The following objects are masked from ‘package:stats’:
#>
#> decompose, spectrum
#> The following object is masked from ‘package:base’:
#>
#> union
tmp1 <- list("apple", "banana") %>% lapply(., `attributes<-`, list(type = "fruit"))
tmp2 <- list("carrot") %>% lapply(., `attributes<-`, list(type = "vegetable"))
append_attr(tmp1, tmp2)
#> [[1]]
#> [1] "apple"
#> attr(,"type")
#> [1] "fruit"
#>
#> [[2]]
#> [1] "banana"
#> attr(,"type")
#> [1] "fruit"
#>
#> [[3]]
#> [1] "carrot"
#> attr(,"type")
#> [1] "vegetable"
#>
rm(tmp1, tmp2)