useR's, First, I would like to say thanks to John Fox for providing this segment of code to perform intersection for multiple sets: intersection <- function(x, y, ...){ if (missing(...)) intersect(x, y) else intersect(x, intersection(y, ...)) } I want to execute this function on the rows of a matrix I have: Ik.mat.test <- matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3)> Ik.mat.test[,1] [,2] [,3] [1,] 2 3 6 [2,] 1 2 6 [3,] 6 1 2 I need to find a way to run the intersection function on the rows of this matrix. The result should be a vector containing 2, 6. This works, but I want to find a way to do this for any size matrix. intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) So, if the user supplied a matrix with any number of rows, I would like to be able to run this function. Any ideas? I have tried something like do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) but this doesnt work Thanks in advance. Derek -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.html Sent from the R help mailing list archive at Nabble.com.
Try this: do.call(intersection, unname(as.data.frame(t(mat)))) On Wed, Feb 20, 2008 at 6:45 PM, dxc13 <dxc13 at health.state.ny.us> wrote:> > useR's, > > First, I would like to say thanks to John Fox for providing this segment of > code to perform intersection for multiple sets: > intersection <- function(x, y, ...){ > if (missing(...)) intersect(x, y) > else intersect(x, intersection(y, ...)) > } > > I want to execute this function on the rows of a matrix I have: > Ik.mat.test <- matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3) > > Ik.mat.test > [,1] [,2] [,3] > [1,] 2 3 6 > [2,] 1 2 6 > [3,] 6 1 2 > > I need to find a way to run the intersection function on the rows of this > matrix. The result should be a vector containing 2, 6. > This works, but I want to find a way to do this for any size matrix. > intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) > So, if the user supplied a matrix with any number of rows, I would like to > be able to run this function. Any ideas? > I have tried something like > do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) > but this doesnt work > > Thanks in advance. > Derek > > > > -- > View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
lk.list<-split(lk.mat.test,1:nrow(lk.mat.test)) names(lk.list)<-NULL do.call(intersection,lk.list) 2008/2/20, dxc13 <dxc13 at health.state.ny.us>:> > useR's, > > First, I would like to say thanks to John Fox for providing this segment of > code to perform intersection for multiple sets: > intersection <- function(x, y, ...){ > if (missing(...)) intersect(x, y) > else intersect(x, intersection(y, ...)) > } > > I want to execute this function on the rows of a matrix I have: > Ik.mat.test <- matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3) > > Ik.mat.test > [,1] [,2] [,3] > [1,] 2 3 6 > [2,] 1 2 6 > [3,] 6 1 2 > > I need to find a way to run the intersection function on the rows of this > matrix. The result should be a vector containing 2, 6. > This works, but I want to find a way to do this for any size matrix. > intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) > So, if the user supplied a matrix with any number of rows, I would like to > be able to run this function. Any ideas? > I have tried something like > do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) > but this doesnt work > > Thanks in advance. > Derek > > > > -- > View this message in context: > http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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