[R] matching matrix columns to a vector
jholtman at gmail.com
Mon Nov 24 19:01:00 CET 2008
You can use the arr.ind option of which:
[,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
1 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:
>  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:
> 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
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help