I'm trying to extract a single element from a list in R and store the element with its vector name as a new list.
Let's assume that I have a list called mylist that has an arbitrary amount of named vectors within and the length of each vector can vary, e.g.
mylist <- list("var1" = c(1,2,3), "var2" = c(10,8,4,2), "var3" = c(6,3), ...)
I know I can get an individual element by using either the name or the index of the vector, e.g.
mylist[["var1"]][1]
[1] 1
mylist[[2]][3]
[1] 4
However, these are only the values. What I want is to create a new list from this extracted value and use the original name of the vector where it was extracted from, something like
element1
$var1
[1] 1
element2
$var2
[1] 4
where element1 and element2 are the new lists. Is there an efficient way of doing this?