Question:
Suppose my Environment contains the following objects (dummy names):
abcd # numeric
efgh # dataframe
ijkl # matrix
mnop # character
My goal : to put them in a list automatically, without having to type:
mylist<-list(abcd,efgh,ijkl,mnop)
Is there a function capable of doing this? More precisely, it would be the opposite of the list2env
function.
Answer:
Here's a way to solve the problem.
First, I will create an Environment with the objects described in the question.
set.seed(1234)
e <- new.env()
e$abcd <- rnorm(10)
e$efgh <- data.frame(A = letters[1:5], X = runif(5))
e$ijkl <- matrix(1:24, ncol = 3)
e$mnop <- sample(LETTERS, 10)
Now, you get the objects from the environment e
with the ls
function and then create the list of these objects with mget
.
obj <- ls(envir = e)
lista <- mget(obj, envir = e)
lista