Search code examples
rlistvectornames

convert named vector to list but keep vector names


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!


Solution

  • You can use split().

    split(a, names(a))