Waverley
2008-Mar-09 23:22 UTC
[R] question about matrix one column values matching a vector of values
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
Henrique Dallazuanna
2008-Mar-09 23:35 UTC
[R] question about matrix one column values matching a vector of values
Use %in% indeed of '==' a[a[,1] %in% b,] On 09/03/2008, Waverley <waverley.paloalto at gmail.com> wrote:> 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Bill.Venables at csiro.au
2008-Mar-10 00:53 UTC
[R] question about matrix one column values matching a vector ofvalues
> 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.