TL;DR: what I need is a vectorised version of list()
.
I'm trying to create a list of objects. I don't know how many objects there'll be or what they'll be called, and there may be a large number of them. So I can't just make a list by hand.
However, I know that all the objects will exist in my environment when it comes time to make the list, and I know I will have a character vector containing the names of all the objects.
(These are the outputs of a horrible three-layer for loop that I'm trying to avoid making any bigger. I could just create an empty list and then assign objects to it as the loop creates them, but I'd prefer not to.)
I've searched Google and Stack Overflow, but I can't find a good solution. It seems to me that what I need is a vectorised version of list()
: something that will take a vector of object names, find all those objects and shove them in a list.
list(obj1, obj2, obj3) # Not this. I don't want to name each element individually.
objectsVector <- c("obj1", "obj2", "obj3")
list_vectorised(elements = objectsVector) # I want this instead.
namesVector <- c("anObject", "anotherObject", "yetAnotherObject")
list_vectorised(elements = objectsVector, names = namesVector) # Or better still, this.