I need help with (hopefully) just one more thing. I have been fussing with this for quite some time and have decided just to give up and ask! I want to match a column in a matrix to a vector. I found a "which" command that I thought would be helpful as it does the following:> g=c(1,5,3,2,7) > which(g==5)[1] 2 As the above gave which placement in the g vector corresponded to 5 (the second place), I need this command to give me which column in a matrix matches to a vector. This is just a toy example of what I am trying to do:> t=matrix(1:12,3,4) >v=c(1,2,3) >which(t[,j]==v)This does not work, and with my "real" matrices and vectors, I was getting outputs that did not make sense. These examples are more to give an idea of what I am aiming to accomplish. Thank you for all the help!!
Jagat.K.Sheth at wellsfargo.com
2008-Nov-24 16:59 UTC
[R] matching matrix columns to a vector
How about which(colSums(t-v) == 0) ? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Salas, Andria Kay Sent: Monday, November 24, 2008 10:04 AM To: r-help at r-project.org Subject: [R] matching matrix columns to a vector I need help with (hopefully) just one more thing. I have been fussing with this for quite some time and have decided just to give up and ask! I want to match a column in a matrix to a vector. I found a "which" command that I thought would be helpful as it does the following:> g=c(1,5,3,2,7) > which(g==5)[1] 2 As the above gave which placement in the g vector corresponded to 5 (the second place), I need this command to give me which column in a matrix matches to a vector. This is just a toy example of what I am trying to do:> t=matrix(1:12,3,4) >v=c(1,2,3) >which(t[,j]==v)This does not work, and with my "real" matrices and vectors, I was getting outputs that did not make sense. These examples are more to give an idea of what I am aiming to accomplish. Thank you for all the help!! ______________________________________________ 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.
Since negative values could balance positives, you might need to use: which(colSums(abs(t-v)) == 0) > t=matrix(1:12,3,4) > v=c(1,2,3) > which(colSums(t-v) == 0) [1] 1 > t [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > t[1,2] <- -5 > which(colSums(t-v) == 0) [1] 1 2 > which(colSums(abs(t-v)) == 0) [1] 1 -- David Winsemius Heritage Labs On Nov 24, 2008, at 11:59 AM, <Jagat.K.Sheth at wellsfargo.com> <Jagat.K.Sheth at wellsfargo.com > wrote:> How about which(colSums(t-v) == 0) ? > > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org > ] > On Behalf Of Salas, Andria Kay > Sent: Monday, November 24, 2008 10:04 AM > To: r-help at r-project.org > Subject: [R] matching matrix columns to a vector > > I need help with (hopefully) just one more thing. I have been fussing > with this for quite some time and have decided just to give up and > ask! > I want to match a column in a matrix to a vector. I found a "which" > command that I thought would be helpful as it does the following: > >> g=c(1,5,3,2,7) >> which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 > (the > second place), I need this command to give me which column in a matrix > matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) >> v=c(1,2,3) >> which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was > getting outputs that did not make sense. These examples are more to > give an idea of what I am aiming to accomplish. > > Thank you for all the help!! > ______________________________________________ > 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. > > ______________________________________________ > 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.
You can use the arr.ind option of which:> t=matrix(1:12,3,4) > v=c(1,2,3) > t[,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12> result <- sapply(v, function(x) which(t == x, arr.ind=TRUE)) > colnames(result) <- v > result1 2 3 # 'v' [1,] 1 2 3 # row [2,] 1 1 1 # column>On Mon, Nov 24, 2008 at 11:03 AM, Salas, Andria Kay <aks2515 at uncw.edu> wrote:> I need help with (hopefully) just one more thing. I have been fussing with this for quite some time and have decided just to give up and ask! I want to match a column in a matrix to a vector. I found a "which" command that I thought would be helpful as it does the following: > >> g=c(1,5,3,2,7) >> which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 (the second place), I need this command to give me which column in a matrix matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) >>v=c(1,2,3) >>which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was getting outputs that did not make sense. These examples are more to give an idea of what I am aiming to accomplish. > > Thank you for all the help!! > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Consider: > apply(t,2,function(x)identical(x,v)) [1] TRUE FALSE FALSE FALSE > apply(t,2,function(x)identical(x,v))+0 [1] 1 0 0 0 and if you wanted to get the position you could use which: > which(apply(t,2,function(x)identical(x,v))) [1] 1 > t[,4] <- v > which(apply(t,2,function(x)identical(x,v))) [1] 1 4 -- David Winsemius On Nov 24, 2008, at 11:03 AM, Salas, Andria Kay wrote:> I need help with (hopefully) just one more thing. I have been > fussing with this for quite some time and have decided just to give > up and ask! I want to match a column in a matrix to a vector. I > found a "which" command that I thought would be helpful as it does > the following: > >> g=c(1,5,3,2,7) >> which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 > (the second place), I need this command to give me which column in a > matrix matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) >> v=c(1,2,3) >> which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was > getting outputs that did not make sense. These examples are more to > give an idea of what I am aiming to accomplish. > > Thank you for all the help!! > ______________________________________________ > 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.
on 11/24/2008 10:03 AM Salas, Andria Kay wrote:> I need help with (hopefully) just one more thing. I have been > fussing with this for quite some time and have decided just to give > up and ask! I want to match a column in a matrix to a vector. I > found a "which" command that I thought would be helpful as it does > the following: > >> g=c(1,5,3,2,7) which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 > (the second place), I need this command to give me which column in a > matrix matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) v=c(1,2,3) which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was > getting outputs that did not make sense. These examples are more to > give an idea of what I am aiming to accomplish. > > Thank you for all the help!!Presuming that you are comparing integers or characters: mat <- matrix(1:12, 3, 4) Vec <- 1:3> mat[,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12> Vec[1] 1 2 3> which(apply(mat, 2, identical, Vec))[1] 1 Another possible option:> which(apply(mat, 2, function(x) all(x == Vec)))[1] 1 See ?apply, ?identical and ?all Note that a variation on this would be required for floating point numbers using all.equal(). You might also want to add error checking to be sure that: length(Vec) == nrow(mat) lest you risk re-cycling of values. HTH, Marc Schwartz
Jagat.K.Sheth wrote:> > How about which(colSums(t-v) == 0) ? >But what about v=c(2,1,3)? It needs to be something like which(colSums((t - v)^2)) == 0 or which(colSums(abs(t - v))) == 0 Dan Jagat.K.Sheth wrote:> > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Salas, Andria Kay > Sent: Monday, November 24, 2008 10:04 AM > To: r-help at r-project.org > Subject: [R] matching matrix columns to a vector > > I need help with (hopefully) just one more thing. I have been fussing > with this for quite some time and have decided just to give up and ask! > I want to match a column in a matrix to a vector. I found a "which" > command that I thought would be helpful as it does the following: > >> g=c(1,5,3,2,7) >> which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 (the > second place), I need this command to give me which column in a matrix > matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) >>v=c(1,2,3) >>which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was > getting outputs that did not make sense. These examples are more to > give an idea of what I am aiming to accomplish. > > Thank you for all the help!! > ______________________________________________ > 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. > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/matching-matrix-columns-to-a-vector-tp20664376p20668707.html Sent from the R help mailing list archive at Nabble.com.
Jagat.K.Sheth at wellsfargo.com
2008-Nov-24 20:08 UTC
[R] matching matrix columns to a vector
Yes! my oversight ... thank you -----Original Message----- From: David Winsemius [mailto:dwinsemius at comcast.net] Sent: Monday, November 24, 2008 11:48 AM To: Sheth, Jagat K Cc: aks2515 at uncw.edu; r-help at r-project.org Subject: Re: [R] matching matrix columns to a vector Since negative values could balance positives, you might need to use: which(colSums(abs(t-v)) == 0) > t=matrix(1:12,3,4) > v=c(1,2,3) > which(colSums(t-v) == 0) [1] 1 > t [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 > t[1,2] <- -5 > which(colSums(t-v) == 0) [1] 1 2 > which(colSums(abs(t-v)) == 0) [1] 1 -- David Winsemius Heritage Labs On Nov 24, 2008, at 11:59 AM, <Jagat.K.Sheth at wellsfargo.com> <Jagat.K.Sheth at wellsfargo.com > wrote:> How about which(colSums(t-v) == 0) ? > > -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org > ] > On Behalf Of Salas, Andria Kay > Sent: Monday, November 24, 2008 10:04 AM > To: r-help at r-project.org > Subject: [R] matching matrix columns to a vector > > I need help with (hopefully) just one more thing. I have been fussing> with this for quite some time and have decided just to give up and > ask! > I want to match a column in a matrix to a vector. I found a "which" > command that I thought would be helpful as it does the following: > >> g=c(1,5,3,2,7) >> which(g==5) > [1] 2 > > As the above gave which placement in the g vector corresponded to 5 > (the second place), I need this command to give me which column in a > matrix matches to a vector. > > This is just a toy example of what I am trying to do: >> t=matrix(1:12,3,4) >> v=c(1,2,3) >> which(t[,j]==v) > > This does not work, and with my "real" matrices and vectors, I was > getting outputs that did not make sense. These examples are more to > give an idea of what I am aiming to accomplish. > > Thank you for all the help!! > ______________________________________________ > 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. > > ______________________________________________ > 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.
Dan Davison wrote:> > > > Jagat.K.Sheth wrote: >> >> How about which(colSums(t-v) == 0) ? >> > > But what about v=c(2,1,3)? It needs to be something like > > which(colSums((t - v)^2)) == 0 > or > which(colSums(abs(t - v))) == 0 >Sorry, apparently I tried to write a line of R code without using emacs. Bad idea. I meant which(colSums((t - v)^2) == 0) Dan Dan Davison wrote:> > > Dan > > > Jagat.K.Sheth wrote: >> >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] >> On Behalf Of Salas, Andria Kay >> Sent: Monday, November 24, 2008 10:04 AM >> To: r-help at r-project.org >> Subject: [R] matching matrix columns to a vector >> >> I need help with (hopefully) just one more thing. I have been fussing >> with this for quite some time and have decided just to give up and ask! >> I want to match a column in a matrix to a vector. I found a "which" >> command that I thought would be helpful as it does the following: >> >>> g=c(1,5,3,2,7) >>> which(g==5) >> [1] 2 >> >> As the above gave which placement in the g vector corresponded to 5 (the >> second place), I need this command to give me which column in a matrix >> matches to a vector. >> >> This is just a toy example of what I am trying to do: >>> t=matrix(1:12,3,4) >>>v=c(1,2,3) >>>which(t[,j]==v) >> >> This does not work, and with my "real" matrices and vectors, I was >> getting outputs that did not make sense. These examples are more to >> give an idea of what I am aiming to accomplish. >> >> Thank you for all the help!! >> ______________________________________________ >> 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. >> >> ______________________________________________ >> 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. >> >> > >-- View this message in context: http://www.nabble.com/matching-matrix-columns-to-a-vector-tp20664376p20668878.html Sent from the R help mailing list archive at Nabble.com.