Re: [R] matching matrix columns to a vector

From: <Jagat.K.Sheth_at_wellsfargo.com>
Date: Mon, 24 Nov 2008 14:08:52 -0600

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.
______________________________________________ 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.
Received on Tue 25 Nov 2008 - 06:52:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Nov 2008 - 07:30:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive