[R] flatten a list to a "true" list

Christian Hoffmann christian.hoffmann at wsl.ch
Tue May 23 14:11:01 CEST 2006


Hi,

I want to flatten a list:

"flatten"(list(a=1,b=2),list(c=3,d=4)) -> L <- list(a=1,b=2,c=3,d=4)

which is

 > L
$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4

 > L[1]
$a
[1] 1

 > L[[1]]
[1] 1

What I used so far is

M <- unlist(c(list(a=1,b=2),list(c=3,d=4))), but this gives

 > M
a b c d
1 2 3 4
 > M[1]
a
1
 > M[[1]]
[1] 1

This shows that L has named "components", but L is a named vector.

What would be the functions

L <- F1(M)
M <- F2(L)

like?

Generally:

Flatten a tree down to level "n", where level 0 is the root.

I searched the net under "flatten", but it showed only unlist.

Thanks for help
Christian

-- 
Dr. Christian W. Hoffmann,
Swiss Federal Research Institute WSL
Mathematics + Statistical Computing
Zuercherstrasse 111
CH-8903 Birmensdorf, Switzerland

Tel +41-44-7392-277  (office)   -111(exchange)
Fax +41-44-7392-215  (fax)
christian.hoffmann at wsl.ch
http://www.wsl.ch/staff/christian.hoffmann

International Conference 5.-7.6.2006 Ekaterinburg Russia
"Climate changes and their impact on boreal and temperate forests"
http://ecoinf.uran.ru/conference/



More information about the R-help mailing list