Alberto Lora M
2009-Jul-20 20:09 UTC
[R] I need to obtain all the rows in m1 in which m2 the elements of m2 are present
Hi could you yelp me please with this Suppose that we have the following matrix m1<-matrix(c("a","7","a","i","o","u","i","1","2","3","4","5","6","7"), ncol=2) m1 [,1] [,2] [1,] "a" "1" [2,] "7" "2" [3,] "a" "3" [4,] "i" "4" [5,] "o" "5" [6,] "u" "6" [7,] "i" "7" m2<-c("a","7") I need to obtain all the rows in m1 in which m2 the elements of m2 are present [,1] [,2] [1,] "a" "1" [2,] "7" "2" [3,] "a" "3" [7,] "i" "7" Thx again -- Alberto Lora Michiels Rue du Progrès, 6B 7860 Lessines GSM 32(0)496659457 [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Jul-20 21:08 UTC
[R] I need to obtain all the rows in m1 in which m2 the elements of m2 are present
Hi Alberto, One way would be index <- apply(m1, 1, function(x) any(x %in% m2)) m1[ index, ] See ?apply, ?any and ?"%in%" for more details. HTH, Jorge On Mon, Jul 20, 2009 at 4:09 PM, Alberto Lora M <albertoloram@gmail.com>wrote:> Hi could you yelp me please with this > > Suppose that we have the following matrix > > m1<-matrix(c("a","7","a","i","o","u","i","1","2","3","4","5","6","7"), > ncol=2) > m1 > [,1] [,2] > [1,] "a" "1" > [2,] "7" "2" > [3,] "a" "3" > [4,] "i" "4" > [5,] "o" "5" > [6,] "u" "6" > [7,] "i" "7" > m2<-c("a","7") > I need to obtain all the rows in m1 in which m2 the elements of m2 are > present > [,1] [,2] > [1,] "a" "1" > [2,] "7" "2" > [3,] "a" "3" > [7,] "i" "7" > > Thx again > > > -- > Alberto Lora Michiels > Rue du Progrès, 6B > 7860 Lessines > GSM 32(0)496659457 > > [[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]]
Henrique Dallazuanna
2009-Jul-20 21:18 UTC
[R] I need to obtain all the rows in m1 in which m2 the elements of m2 are present
Try this also: m1[as.logical(rowSums(matrix(m1 %in% m2, ncol = ncol(m1)))),] On Mon, Jul 20, 2009 at 5:09 PM, Alberto Lora M <albertoloram@gmail.com>wrote:> Hi could you yelp me please with this > > Suppose that we have the following matrix > > m1<-matrix(c("a","7","a","i","o","u","i","1","2","3","4","5","6","7"), > ncol=2) > m1 > [,1] [,2] > [1,] "a" "1" > [2,] "7" "2" > [3,] "a" "3" > [4,] "i" "4" > [5,] "o" "5" > [6,] "u" "6" > [7,] "i" "7" > m2<-c("a","7") > I need to obtain all the rows in m1 in which m2 the elements of m2 are > present > [,1] [,2] > [1,] "a" "1" > [2,] "7" "2" > [3,] "a" "3" > [7,] "i" "7" > > Thx again > > > -- > Alberto Lora Michiels > Rue du Progrès, 6B > 7860 Lessines > GSM 32(0)496659457 > > [[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. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]