[R] convert list without same component length to matrix

arun smartpink111 at yahoo.com
Thu Nov 1 13:29:02 CET 2012



Hi,
You can also use:
sapply(l,`[`,1:4)
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1    3    4    2    3
#[2,]    2    4    2    4    5
#[3,]    3    5    5    6    7
#[4,]    7    6    7    3    2
A.K.



----- Original Message -----
From: D. Rizopoulos <d.rizopoulos at erasmusmc.nl>
To: Al Ehan <aehan3616 at gmail.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Thursday, November 1, 2012 4:19 AM
Subject: Re: [R] convert list without same component length to matrix

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/
______________________________________________
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.





More information about the R-help mailing list