Hi All, I have a list of matrices:> x[,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6> y[,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> z =list(x,y)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 -- Federico C. F. Calboli Department of Epidemiology and Public Health Imperial College, St Mary's Campus Norfolk Place, London W2 1PG Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 f.calboli [.a.t] imperial.ac.uk f.calboli [.a.t] gmail.com
Try this: lapply(z, "[", 2:3, TRUE) On 2/28/06, Federico Calboli <f.calboli at imperial.ac.uk> wrote:> Hi All, > > I have a list of matrices: > > > x > [,1] [,2] > [1,] 1 4 > [2,] 2 5 > [3,] 3 6 > > y > [,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 > > z =list(x,y) > > 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 > > -- > Federico C. F. Calboli > Department of Epidemiology and Public Health > Imperial College, St Mary's Campus > Norfolk Place, London W2 1PG > > Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 > > f.calboli [.a.t] imperial.ac.uk > f.calboli [.a.t] gmail.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Federico Calboli wrote:> Hi All, > > I have a list of matrices: > > >>x > > [,1] [,2] > [1,] 1 4 > [2,] 2 5 > [3,] 3 6 > >>y > > [,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 > >>z =list(x,y) > > > 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: 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:> Hi All, > > I have a list of matrices: > > > x > [,1] [,2] > [1,] 1 4 > [2,] 2 5 > [3,] 3 6 > > y > [,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 > > z =list(x,y) > > 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 CalboliHere is one approach:> lapply(z, function(x) x[2:3, ])[[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> lapply(z, function(s) s[2:3,,drop=F])[[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:> Hi All, > > I have a list of matrices: > > > x > [,1] [,2] > [1,] 1 4 > [2,] 2 5 > [3,] 3 6 > > y > [,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 > > z =list(x,y) > > 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 > > -- > Federico C. F. Calboli > Department of Epidemiology and Public Health > Imperial College, St Mary's Campus > Norfolk Place, London W2 1PG > > Tel +44 (0)20 7594 1602 Fax (+44) 020 7594 3193 > > f.calboli [.a.t] imperial.ac.uk > f.calboli [.a.t] gmail.com > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >