Hi all, If I have a huge matrix/ dataframe and I want to create a new matrix/ dataframe with every second (or third, or fourth etc.) row of the original matrix, how can I do it? Any help much appreciated. Thanks, Umesh [[alternative HTML version deleted]]
Try this,> a = matrix(rnorm(10*10),ncol=10) > a[, seq(1,ncol(a),by=2)]baptiste On 29 Apr 2009, at 09:28, Umesh Srinivasan wrote:> Hi all, > > If I have a huge matrix/ dataframe and I want to create a new matrix/ > dataframe with every second (or third, or fourth etc.) row of the > original > matrix, how can I do it? Any help much appreciated. > > Thanks, > Umesh > > [[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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
probably you want to use seq(), e.g., mat <- matrix(1:500, 100, 5) mat[seq(1, nrow(mat), 2), ] mat[seq(1, nrow(mat), 3), ] mat[seq(1, nrow(mat), 4), ] I hope it helps. Best, Dimitris Umesh Srinivasan wrote:> Hi all, > > If I have a huge matrix/ dataframe and I want to create a new matrix/ > dataframe with every second (or third, or fourth etc.) row of the original > matrix, how can I do it? Any help much appreciated. > > Thanks, > Umesh > > [[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
On Wed, Apr 29, 2009 at 01:58:06PM +0530, Umesh Srinivasan wrote:> Hi all, > > If I have a huge matrix/ dataframe and I want to create a new matrix/ > dataframe with every second (or third, or fourth etc.) row of the original > matrix, how can I do it? Any help much appreciated.Others ahve already poitned out soltions using seq9(). I'd just like to add an alternative approach making use of R's logical indexing and recycling rules: mat <- matrix(1:1000, 100, 5) mat2 <- mat[c(T,F),] cu Philipp -- Dr. Philipp Pagel Lehrstuhl f?r Genomorientierte Bioinformatik Technische Universit?t M?nchen Wissenschaftszentrum Weihenstephan 85350 Freising, Germany http://mips.gsf.de/staff/pagel