[R] Creating lists from matrices

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 25 10:31:11 CET 2004


my.matrix <- matrix(LETTERS[1:9],3,3,byrow=TRUE)
split(my.matrix, row(my.matrix))

$"1"
[1] "A" "B" "C"

$"2"
[1] "D" "E" "F"

$"3"
[1] "G" "H" "I"

which even names the rows for you.

On Thu, 25 Nov 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
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list