Zhenjiang Lan
2012-Jun-12 15:28 UTC
[R] How to index a matrix with different row-number for each column?
here's my question: suppose I have a matrix: mt<-matrix(1:12,ncol=6) now I have a vector vt<-c(1,2,2,2,1,2) which means I want to get: the 1st row for column1; the 2nd row for column2; the 2nd row for column3; the 2nd row for column4; ... that what I want is this vector: 1,4,6,8,9,12 Does anyone know how to do this fast? I know I can use for-loop to travel all columns,but that's not what I want.
R. Michael Weylandt
2012-Jun-12 15:32 UTC
[R] How to index a matrix with different row-number for each column?
I think you are looking for mt[cbind(vt, seq_along(vt))] which uses some trickines known as matrix indexing (basically, the two columns created by cbind() are considered row & col indices -- then seq_along() gives the col1,col2, bit as needed. HTH, Michael On Tue, Jun 12, 2012 at 10:28 AM, Zhenjiang Lan <lan.zhenjiang at gmail.com> wrote:> here's my question: suppose I have a matrix: > > ? ?mt<-matrix(1:12,ncol=6) > > now I have a vector > > ? ?vt<-c(1,2,2,2,1,2) > > which means I want to get: > the 1st row for column1; > the 2nd row for column2; > the 2nd row for column3; > the 2nd row for column4; > ... > > that what I want is this vector: > 1,4,6,8,9,12 > > Does anyone know how to do this fast? > I know I can use for-loop to travel all columns,but that's not what I want. > > ______________________________________________ > 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.
William Dunlap
2012-Jun-12 15:33 UTC
[R] How to index a matrix with different row-number for each column?
Use a 2-column matrix of integer subscripts, the first column being the row indexes and the the second the column indexes: > mt<-matrix(1:12,ncol=6) > vt<-c(1,2,2,2,1,2) > mt[cbind(vt,1:6)] [1] 1 4 6 8 9 12 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Zhenjiang Lan > Sent: Tuesday, June 12, 2012 8:28 AM > To: r-help at r-project.org > Subject: [R] How to index a matrix with different row-number for each column? > > here's my question: suppose I have a matrix: > > mt<-matrix(1:12,ncol=6) > > now I have a vector > > vt<-c(1,2,2,2,1,2) > > which means I want to get: > the 1st row for column1; > the 2nd row for column2; > the 2nd row for column3; > the 2nd row for column4; > ... > > that what I want is this vector: > 1,4,6,8,9,12 > > Does anyone know how to do this fast? > I know I can use for-loop to travel all columns,but that's not what I want. > > ______________________________________________ > 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.
Nordlund, Dan (DSHS/RDA)
2012-Jun-12 15:40 UTC
[R] How to index a matrix with different row-number for each column?
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Zhenjiang Lan > Sent: Tuesday, June 12, 2012 8:28 AM > To: r-help at r-project.org > Subject: [R] How to index a matrix with different row-number for each > column? > > here's my question: suppose I have a matrix: > > mt<-matrix(1:12,ncol=6) > > now I have a vector > > vt<-c(1,2,2,2,1,2) > > which means I want to get: > the 1st row for column1; > the 2nd row for column2; > the 2nd row for column3; > the 2nd row for column4; > ... > > that what I want is this vector: > 1,4,6,8,9,12 > > Does anyone know how to do this fast? > I know I can use for-loop to travel all columns,but that's not what I > want. >There may be a more memory efficient method, but this will get the job done. diag(mt[vt,1:6]) Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Zhenjiang Lan
2012-Jun-12 20:55 UTC
[R] How to index a matrix with different row-number for each column?
Thank you, it works. by the way, sometime I guess *apply functions act like loop, because when I apply a complicated function, they run slowly and one by one. On Tue, Jun 12, 2012 at 3:39 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote:> Hello, > > Try > > sapply(seq_len(ncol(mt2)), function(j) mt1[mt2[, j], j]) > > > Hope this helps, > > Rui Barradas > > Em 12-06-2012 17:19, Zhenjiang Lan escreveu: > >> mt1: >> ? ? ?[,1] [,2] [,3] >> [1,] "G" ?"A" ?"A" >> [2,] "C" ?"T" ?"C" >> >> mt2: >> ? ? ?[,1] [,2] [,3] >> [1,] ? ?1 ? ?2 ? ?2 >> [2,] ? ?2 ? ?1 ? ?2 >> [3,] ? ?2 ? ?2 ? ?2 >> [4,] ? ?1 ? ?1 ? ?1 > >
Seemingly Similar Threads
- Function to read a string as the variables as opposed to taking the string name as the variable
- Displaying median value over the horizontal(median)line in the boxplot
- R Shiny Help - Trouble passing user input columns to emmeans after ANOVA analysis
- How to separate the string?
- Some SQL Challenges