0

I want to transform the elements of a list into independent vectors that are saved in and usably from the global environment.

Imagine a list "the.list" that consists of three elements.

$a
[1] 1 2 3
$b
[1] 6 7 8
$c
[1] 11 12 13

Is there a way of storing each of the elements as individual vectors?

I have tried to use lapply (the.list, as.vector) , but the result is the same as simply printing the list. Also, using unlist (the.list) returns a simple vector of 1:15, in which the distinct sets (1:3, 6:8, 11:13) are non distinguishable.

I am looking for a function/way to transform the elements of the list into what would would manually be

a<-c(1:3)
b<-c(6:8)
c<-c(11:13) 

Many thanks.

sant
  • 91
  • 6

1 Answers1

1
# Creating some data
my_list <- list(a = c(1:3), b = c(4:5), c = c(6:10))

# Assigning each vector to element name    
for (i in 1:length(my_list)) {
  assign(names(my_list[i]), as.vector(my_list[i]))
}
Esben Eickhardt
  • 3,183
  • 2
  • 35
  • 56