[R] slicing list with matrices

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Wed Nov 17 15:13:23 CET 2010


try this:

m <- matrix(1:9, nrow=3, dimnames=list(LETTERS[1:3], letters[1:3]))
l <- list(m1=m, m2=m*2, m3=m*3)

lapply(l, "[", 1, 1)

# or

sapply(l, "[", 1, 1)


I hope it helps.

Best,
Dimitris


On 11/17/2010 3:01 PM, soeren.vogel at eawag.ch wrote:
> A list contains several matrices. Over all matrices (list elements) I'd like to access one matrix cell:
>
> m<- matrix(1:9, nrow=3, dimnames=list(LETTERS[1:3], letters[1:3]))
> l<- list(m1=m, m2=m*2, m3=m*3)
> l[[3]] # works
> l[[3]][1:2, ] # works
> l[[1:3]][1, 1] # does not work
>
> How can I slice all C-c combinations in the list?
>
> Sören
>

-- 
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