I want to add elements to a list using existing slots of this very list. In base R I would use the following syntax:
l <- list(a = 1, b = 2, e = 1, f = 1)
## Add slot c and d which take the values from a and b respectively
l$c <- l$a
l$d <- l$b
str(l)
# List of 6
# $ a: num 1
# $ b: num 2
# $ e: num 1
# $ f: num 1
# $ c: num 1
# $ d: num 2
Avoiding l$ can be done via attach / with / within which I do not want to use.
So what is the canonical way of doing that with tidyverse? One solution I could think of is:
library(magrittr)
library(purrr)
l <- list(a = 1, b = 2, e = 1, f = 1)
str(l %$% list_modify(l, c = a, d = b))
str(l)
# List of 6
# $ a: num 1
# $ b: num 2
# $ e: num 1
# $ f: num 1
# $ c: num 1
# $ d: num 2
But as there are plenty of functions in the tidyverse I was wondering whether there is afunction which is "more" fit for purpose, i.e. using maybe less different libraries / function calls?
Update. Adapted the example slightly to highlight that not all columns will be copied.