user1160354
user1160354

Reputation: 617

the elements of a character vector are lists - how to combine them

given that I have a vector: x <- c("m1", "m2", "m3") and every element m1, m2 and m3 is a list e.g.

m1 = list(a=1:3, b=2:4, c=1:10)  
m2 = list(a=0:3, b=0:4, c=6:10)  
m3 = list(a=1:30, b=1:2, c=6:10)  

I want to be able to create "super list" using loop:

mylist <- list()  
for(i in x)mylist[[i]] <- ...??....  

when i="m1" then mylist[["m1"]] <- m1

any suggestion would be appreciate.

Robert

Upvotes: 1

Views: 402

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57696

It looks like what you're trying to do is, given a vector of object names, combine the given objects into a list.

mylist <- lapply(x, get)
names(mylist) <- x

Upvotes: 3

Related Questions