Quantcast

Select specific rows of a matrix

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Select specific rows of a matrix

syrvn
Hi,

if I have a large matrix e.g. with the dimensions of 1000 rows and 10 columns.
How can I select rows comprising one or more values between a specific range of two values?
So e.g. a row has one or more values between the range of 100 - 120.

Cheers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Select specific rows of a matrix

dusadrian
mentor_ wrote
Hi,

if I have a large matrix e.g. with the dimensions of 1000 rows and 10 columns.
How can I select rows comprising one or more values between a specific range of two values?
So e.g. a row has one or more values between the range of 100 - 120.
No tested, but this should work (say your matrix is called "mat"):

mat[apply(mat, 1, function(x) any(x > 99 & x < 121)), ]

Hth,
Adrian
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Select specific rows of a matrix

D. Rizopoulos
In reply to this post by syrvn
try this:

mat <- matrix(sample(90:2000, 10000, TRUE), 1000, 10)

ind <- rowSums(mat > 100 & mat < 120) > 0
mat[ind, ]


I hope it helps.

Best,
Dimitris


mentor_ wrote:
> Hi,
>
> if I have a large matrix e.g. with the dimensions of 1000 rows and 10
> columns.
> How can I select rows comprising one or more values between a specific range
> of two values?
> So e.g. a row has one or more values between the range of 100 - 120.
>
> Cheers

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
[hidden email] 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.
Loading...