[R] Convert a matrix's columns to list

Vito Muggeo (UniPa) vito.muggeo at unipa.it
Tue Jan 18 14:13:49 CET 2011


hi feng,
a possible solution is

b1<-apply(a,2,list)

and possibly

lapply(b1,unlist)

if you want exactly the output equal to "list(a[, 1], a[, 2])"

best,
vito

Il 18/01/2011 13.53, Feng Li ha scritto:
> Dear R,
>
> Is there an efficient way to make a list that each element is from the
> corresponding column of a matrix. For example, if I have a matrix "a"
>
>> a<- matrix(1:10, 5, 2)
>> a
>       [,1] [,2]
> [1,]    1    6
> [2,]    2    7
> [3,]    3    8
> [4,]    4    9
> [5,]    5   10
>
> I would like to have a list "b" like this
>
>> b<- list(a[, 1], a[, 2])
>> b
> [[1]]
> [1] 1 2 3 4 5
>
> [[2]]
> [1]  6  7  8  9 10
>
>
> Thanks in advance!
>
>
> Feng
>

-- 
====================================
Vito M.R. Muggeo
Dip.to Sc Statist e Matem `Vianelli'
Università di Palermo
viale delle Scienze, edificio 13
90128 Palermo - ITALY
tel: 091 23895240
fax: 091 485726/485612
http://dssm.unipa.it/vmuggeo



More information about the R-help mailing list