I'm trying to combine three vectors into a list of lists, with one vector being the indexes for the list.
Given
names<-c("a","b","c","d")
id1<-c(1,1,2,2)
id2<-c(3,4,5,6)
I would like to build a list looking like this:
mylist <- list("a" = list(1,3), "b" = list(1,4), "c" = list(2,5), "d" = list(2,6))
I think you are looking for Map
l <- Map(list, id1, id2) # thanks to @thelatemail for suggested improvement
names(l) <- names
identical(mylist, l)
# TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With