Prof Brian D Ripley
2000-Oct-14 17:26 UTC
[R] Re: I want to pull out an element from each of a list of matrices
On Sat, 14 Oct 2000, niels Waller wrote:> Dear colleagues, > > I suspect there is a simple answer to this question -- but I cannot find it. > > Suppose I have a list of matrices. I want to pull out an element (such as > row 1, col 2) from each matrix. Do I need a loop to do this? Or is there an > easy way to index this? > > Let Z denote the list of matrices. All matrices have the same order. I > want something like > Z[[.]][1,2] (which of course will not work, but you get the idea)lapply(Z, function(x) x[1,2]) should do this, giving a list. Use sapply if you want a vector. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, 14 Oct 2000, niels Waller wrote:> > > > Dear colleagues, > > I suspect there is a simple answer to this question -- but I cannot find it. > > Suppose I have a list of matrices. I want to pull out an element (such as > row 1, col 2) from each matrix. Do I need a loop to do this? Or is there an > easy way to index this? > > Let Z denote the list of matrices. All matrices have the same order. I > want something like > Z[[.]][1,2] (which of course will not work, but you get the idea) >lapply(Z,function(zi) zi[1,2]) will work. You may want to wrap this in unlist() to get a vector instead of a list. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear colleagues, I suspect there is a simple answer to this question -- but I cannot find it. Suppose I have a list of matrices. I want to pull out an element (such as row 1, col 2) from each matrix. Do I need a loop to do this? Or is there an easy way to index this? Let Z denote the list of matrices. All matrices have the same order. I want something like Z[[.]][1,2] (which of course will not work, but you get the idea) Thanks for any and all tips, Niels Waller Professor of Quantitative Methods and Evaluation Department of Psychology and Human Development Vanderbilt University niels.waller at vanderbilt.edu -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._