Hi, This might seem like a simple question but at the moment I am stuck for ideas. The columns of my matrix in which some data is stored are of this form: X1 Y1 X2 Y2 X3 Y3 ... Xn Yn with n~100. I would like to look at just the X values (i.e. odd column numbers). Is there an easy way to loop round extracting only these columns? Any help would be appreciated. Thank you.
oddvals <- seq(1, ncol(mydata), by=2) mydata[,oddvals] On Wed, Mar 9, 2011 at 9:20 AM, Nixon, Matthew <M.R.Nixon at exeter.ac.uk> wrote:> Hi, > > This might seem like a simple question but at the moment I am stuck for ideas. The columns of my matrix in which some data is stored are of this form: > > X1 ? ? Y1 ? ? X2 ? ? Y2 ? ? X3 ? ? Y3 ? ? ... ? ? Xn ? ? Yn > > with n~100. I would like to look at just the X values (i.e. odd column numbers). Is there an easy way to loop round extracting only these columns? > > Any help would be appreciated. > > Thank you.-- Sarah Goslee http://www.functionaldiversity.org
one way is using the seq(), e.g., say 'm' is your matrix, then try: m[, seq(1, ncol(m), by = 2)] I hope it helps. Best, Dimitris On 3/9/2011 3:20 PM, Nixon, Matthew wrote:> Hi, > > This might seem like a simple question but at the moment I am stuck for ideas. The columns of my matrix in which some data is stored are of this form: > > X1 Y1 X2 Y2 X3 Y3 ... Xn Yn > > with n~100. I would like to look at just the X values (i.e. odd column numbers). Is there an easy way to loop round extracting only these columns? > > Any help would be appreciated. > > Thank you. > ______________________________________________ > 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 Web: http://www.erasmusmc.nl/biostatistiek/
Try this: m <- matrix(rnorm(12), ncol = 4) m[,c(FALSE, TRUE)] On Wed, Mar 9, 2011 at 11:20 AM, Nixon, Matthew <M.R.Nixon@exeter.ac.uk>wrote:> Hi, > > This might seem like a simple question but at the moment I am stuck for > ideas. The columns of my matrix in which some data is stored are of this > form: > > X1 Y1 X2 Y2 X3 Y3 ... Xn Yn > > with n~100. I would like to look at just the X values (i.e. odd column > numbers). Is there an easy way to loop round extracting only these columns? > > Any help would be appreciated. > > Thank you. > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Bill.Venables at csiro.au
2011-Mar-10 01:40 UTC
[R] Extracting only odd columns from a matrix
Xonly <- XY[, grep("^X", dimnames(XY)[[2]])] -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Nixon, Matthew Sent: Thursday, 10 March 2011 12:20 AM To: r-help at R-project.org Subject: [R] Extracting only odd columns from a matrix Hi, This might seem like a simple question but at the moment I am stuck for ideas. The columns of my matrix in which some data is stored are of this form: X1 Y1 X2 Y2 X3 Y3 ... Xn Yn with n~100. I would like to look at just the X values (i.e. odd column numbers). Is there an easy way to loop round extracting only these columns? Any help would be appreciated. Thank you. ______________________________________________ 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.