[R] Extracting only odd columns from a matrix

rex.dwyer at syngenta.com rex.dwyer at syngenta.com
Wed Mar 9 18:45:46 CET 2011


Or, if X1 Y1 X2 Y2... are really your column names
m[, grep("X",colnames(m)) ]
or
m[, grepl("X",colnames(m)) ]

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dimitris Rizopoulos
Sent: Wednesday, March 09, 2011 9:36 AM
To: Nixon, Matthew
Cc: r-help at R-project.org
Subject: Re: [R] Extracting only odd columns from a matrix

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/

______________________________________________
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.




message may contain confidential information. If you are not the designated recipient, please notify the sender immediately, and delete the original and any copies. Any use of the message by you is prohibited. 


More information about the R-help mailing list