[R] Creating lists from matrices

Eric Lecoutre lecoutre at stat.ucl.ac.be
Thu Nov 25 10:24:52 CET 2004


Hi,

One could use the following:

 >  mm=matrix(letters[1:9],ncol=3,byrow=TRUE)
 >  lapply(apply(mm,1,list),function(el)el[[1]])
[[1]]
[1] "a" "b" "c"

[[2]]
[1] "d" "e" "f"

[[3]]
[1] "g" "h" "i"

You could also have a look at as.data.frame.matrix, which transform a 
matrix into a data.frame efficiently. data.frames are internaly structured 
as lists...

Eric


At 10:12 25/11/2004, Alexander Sokol wrote:
>Hello,
>
>I am using R 1.9.1 on Windows 2000 SP4. I have the following problem:
>
>Say I have a matrix,
>
> >my.matrix
>        [,1]   [,2]   [,3]
>[1,]   "A"   "B"   "C"
>[2,]   "D"   "E"   "F"
>[3,]   "G"   "H"   "I"
>
>I would like to apply an operation to this matrix which returns a list 
>my.list
>containing the following 3 elements,
>
> >my.list
>[[1]]
>[1] "A" "B" "C"
>[[2]]
>[2] "D" "E" "F"
>[[3]]
>[3] "G" "H" "I"
>
>That is, each row of the original matrix is turned into a vector and these
>vectors are collected to a list. How do I do this?
>
>Thanks,
>  Alexander
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte




More information about the R-help mailing list