Hi all! I have a vector, let's say for example int <- sample(1:20,10); for now: now I have a matrix... M = m x n where the first column is a "feature" column and most likely shares at least one of the int (interesting) numbers. I want to extract the rows where int[] = M[,1] I thought: rownames(int)<-int; rownames(M)<-M[,1]; M[rownames(int),] would work, but it doesn't... (I assume because I have rownames(int) that are not found in M[,1]. Neither does, rownames(M)==rownames(int)... Any help would be greatly appreciated! Thank you! [[alternative HTML version deleted]]
Hi: Here' s one approach:> int <- sample(1:20,10) > m <- matrix(sample(1:40, 20), nrow = 10) > int[1] 7 12 4 6 1 19 17 20 15 5> m[,1] [,2] [1,] 9 15 [2,] 23 32 [3,] 40 14 [4,] 19 38 [5,] 28 6 [6,] 26 18 [7,] 34 22 [8,] 7 35 [9,] 21 3 [10,] 39 12> m[m[, 1] %in% int, ][,1] [,2] [1,] 19 38 [2,] 7 35 HTH, Dennis On Fri, Apr 1, 2011 at 10:08 PM, Joseph N. Paulson <josephpaulson@gmail.com>wrote:> Hi all! > > I have a vector, let's say for example int <- sample(1:20,10); > for now: > > now I have a matrix... > M = m x n > where the first column is a "feature" column and most likely shares at > least > one of the int (interesting) numbers. > > I want to extract the rows where int[] = M[,1] > > I thought: > rownames(int)<-int; > rownames(M)<-M[,1]; > > M[rownames(int),] would work, but it doesn't... (I assume because I have > rownames(int) that are not found in M[,1]. Neither does, > rownames(M)==rownames(int)... > > Any help would be greatly appreciated! > > Thank you! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
HI, here is another solution: int <- sample(1:20,10) int [1] 10 4 5 2 14 17 9 11 16 13 mat<-matrix(11:30,ncol=4) mat [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 12 17 22 27 [3,] 13 18 23 28 [4,] 14 19 24 29 [5,] 15 20 25 30 mat[apply(mat,1, function(x) any(int==x[1])),] [,1] [,2] [,3] [,4] [1,] 11 16 21 26 [2,] 13 18 23 28 [3,] 14 19 24 29 Andrija On Sat, Apr 2, 2011 at 7:08 AM, Joseph N. Paulson <josephpaulson@gmail.com>wrote:> Hi all! > > I have a vector, let's say for example int <- sample(1:20,10); > for now: > > now I have a matrix... > M = m x n > where the first column is a "feature" column and most likely shares at > least > one of the int (interesting) numbers. > > I want to extract the rows where int[] = M[,1] > > I thought: > rownames(int)<-int; > rownames(M)<-M[,1]; > > M[rownames(int),] would work, but it doesn't... (I assume because I have > rownames(int) that are not found in M[,1]. Neither does, > rownames(M)==rownames(int)... > > Any help would be greatly appreciated! > > Thank you! > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]