[R] convert list without same component length to matrix

D. Rizopoulos d.rizopoulos at erasmusmc.nl
Thu Nov 1 09:19:55 CET 2012


try this:

l <- list(c(1,2,3,7), c(3,4,5,6,3), c(4,2,5,7), c(2,4,6,3,2), c(3,5,7,2))

sapply(l, head, 4)


I hope it helps.

Best,
Dimitris


On 11/1/2012 9:11 AM, Al Ehan wrote:
> Hi,
>
> I have this lame question. I want to convert a list (each with varies in
> length) to matrix with same row length by eliminating vectors outside the
> needed range.
> For example:
>
> l<-list(NULL)
> l[[1]]=1,2,3.7
> l[[2]]=3,4,5,6,3
> l[[3]]=4,2,5,7
> l[[4]]=2,4,6,3,2
> l[[5]]=3,5,7,2
>
>
> #so say I want to only have 4 rows and 5 column in my matrix (or
> data.frame) and eliminating the 5th index value in l[[2]] and l[[4]]
> #what is the simplest code would be? I actually have hundreds of the list
> components.
>
> thanks
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list