Reputation: 360
I have a named vector that I want to convert to a list, as such:
a = 1:10
names(a) = letters[1:10]
as.list(a)
$a
[1] 1
$b
[1] 2
$c
[1] 3
Here, the names of each vector is now the name of the list, but I need the vectors within the list to keep their names, like this:
as.list(a)
$a
a
1
$b
b
2
$c
c
3
Any ideas? Thanks!
Upvotes: 4
Views: 1304
Reputation: 887851
One option after going ahead with as.list
is to the set the names of the elements with the corresponding names
of original vector
with Map
Map(setNames, as.list(a), names(a))
#$a
#a
#1
#$b
#b
#2
#$c
#c
#3
#$d
#d
#4
#$e
#e
#5
#$f
#f
#6
#$g
#g
#7
#$h
#h
#8
#$i
#i
#9
#$j
# j
#10
Upvotes: 0