Skip to content

subsetting a list of matrices

5 messages · Federico Calboli, Gabor Grothendieck, Sundar Dorai-Raj +2 more

#
Hi All,

I have a list of matrices:
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]   18   21   24   27   30   33
[2,]   19   22   25   28   31   34
[3,]   20   23   26   29   32   35
I want to create a second list that is has a subset each matrix in the
list subsetting so I get the 2nd and 3rd row of each (and all columns).

How could I do that (apart from looping)?

Regards,

Federico Calboli
#
Try this:

lapply(z, "[", 2:3, TRUE)
On 2/28/06, Federico Calboli <f.calboli at imperial.ac.uk> wrote:
#
Federico Calboli wrote:
Try:

x <- matrix(1:6, 3, 2)
y <- matrix(18:35, 3, 6)
z <- list(x = x, y = y)
lapply(z, "[", 2:3, TRUE)

or

lapply(z, "[", 2:3, TRUE, drop = FALSE)

to prevent "[" from dropping the dim attribute. The latter is only 
required if

x <- matrix(1:2, 2, 1)

for example.


HTH,

--sundar
#
On Tue, 2006-02-28 at 17:14 +0000, Federico Calboli wrote:
Here is one approach:
[[1]]
     [,1] [,2]
[1,]    2    5
[2,]    3    6

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   19   22   25   28   31   34
[2,]   20   23   26   29   32   35


HTH,

Marc Schwartz
#
Hi. Have you tried 'help.search('list')' ?
See ?lapply
[[1]]
     [,1] [,2]
[1,]    2    5
[2,]    3    6

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   19   22   25   28   31   34
[2,]   20   23   26   29   32   35


Marco Geraci
--- Federico Calboli <f.calboli at imperial.ac.uk> wrote: