[R] matching matrix columns to a vector

Dan Davison davison at stats.ox.ac.uk
Mon Nov 24 21:06:20 CET 2008




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.



More information about the R-help mailing list