[R] question about matrix one column values matching a vector ofvalues

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Mar 10 01:53:56 CET 2008


> a <- matrix (1:16, 4, 4)
> b <- c (2,3) 
> 
> which(a[, 1] %in% b)
[1] 2 3
> 
> a
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14  ## <<----
[3,]    3    7   11   15  ## <<----
[4,]    4    8   12   16
> 

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables at csiro.au
http://www.cmis.csiro.au/bill.venables/ 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Waverley
Sent: Monday, 10 March 2008 9:23 AM
To: r-help; r-help at r-project.org
Subject: [R] question about matrix one column values matching a vector
ofvalues

Hi,

I have a matrix
a = matrix (1:16, 4, 4)
b = c (2,3)

I want to find out which rows of a, where a[,1] equals any values of b?

I know that if b is only one value, e.g, b=2, then what I want is
a[a[,1] == 2,]

But what about if it is not one value but a vector of values?

Thanks much in advance.

-- 
Waverley @ Palo Alto

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



More information about the R-help mailing list