Hi, I've got a matrix of two columns, say:> d <- t(array(1:20,dim=c(2,10))) > d[,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 [4,] 7 8 [5,] 9 10 [6,] 11 12 [7,] 13 14 [8,] 15 16 [9,] 17 18 [10,] 19 20 now I need those values from column #2 where column #1 is equal to certain values. eg.:> i <- c(13,5,19)The following doesn't work but you may get the idea:>print( d[ d[,1]==i ,2])It is possible to do it with a loop:> k <- i # just to allocate k > for (j in 1:3) {k[j] <- which(d[,1]==i[j]))} > print(d[k,2])But I believe there has to be a more efficient way in R without explicit loops, which I don?t know by now ??? Tia. Thomas -- ------------------------------------------------------- Dr. Thomas Bruns <thomas.bruns at ptb.de> Physikalisch-Technische Bundesanstalt Braunschweig Fachlabor: 1.13 Bundesallee 100 Tel: 0531/592 - 1132 D-38116 Braunschweig Fax: 0531/592-69-1132 ------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tuesday 20 March 2001 16:57, Thomas Bruns wrote:> Hi, > > I've got a matrix of two columns, say: > > d <- t(array(1:20,dim=c(2,10))) > > d > > [,1] [,2] > [1,] 1 2 > [2,] 3 4 > [3,] 5 6 > [4,] 7 8 > [5,] 9 10 > [6,] 11 12 > [7,] 13 14 > [8,] 15 16 > [9,] 17 18 > [10,] 19 20 > > now I need those values from column #2 where column #1 is equal to > > certain values. eg.: > > i <- c(13,5,19) > > The following doesn't work but you may get the idea: > >print( d[ d[,1]==i ,2])Try print( d[d[,1] %in% i ,2]) Cheers Kaspar -- Kaspar Pflugshaupt Geobotanical Institute ETH Zurich, Switzerland http://www.geobot.umnw.ethz.ch mailto:pflugshaupt at geobot.umnw.ethz.ch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thomas Bruns wrote:> I've got a matrix of two columns, say: > > d <- t(array(1:20,dim=c(2,10))) > ... > now I need those values from column #2 where column #1 is equal to > certain values. eg.: > > i <- c(13,5,19) > The following doesn't work but you may get the idea: > >print( d[ d[,1]==i ,2])Hello Thomas, what about:> d <- t(array(1:20,dim=c(2,10))) > i <- c(13,5,19) > d[match(i,d[,1]), 2][1] 14 6 20 Peter ------------------------------------------------------------ Dr. Peter Wolf pwolf at wiwi.uni-bielefelde.de Statistik/Informatik Wirtschaftswissenschaften Uni Bielefeld Bielefeld Germany ------------------------------------------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
try d[d[,1] %in% i,]> d[d[,1] %in% i,][,1] [,2] [1,] 5 6 [2,] 13 14 [3,] 19 20>R. Woodrow Setzer, Jr. Phone: (919) 541-0128 Experimental Toxicology Division Fax: (919) 541-5394 Pharmacokinetics Branch NHEERL MD-74; US EPA; RTP, NC 27711 Thomas Bruns <Thomas.Bruns at ptb.de To: R-help <r-help at stat.math.ethz.ch> > cc: Sent by: Subject: [R] finding dedicated indexes? owner-r-help at stat.ma th.ethz.ch 03/20/01 10:57 AM Hi, I've got a matrix of two columns, say:> d <- t(array(1:20,dim=c(2,10))) > d[,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 [4,] 7 8 [5,] 9 10 [6,] 11 12 [7,] 13 14 [8,] 15 16 [9,] 17 18 [10,] 19 20 now I need those values from column #2 where column #1 is equal to certain values. eg.:> i <- c(13,5,19)The following doesn't work but you may get the idea:>print( d[ d[,1]==i ,2])It is possible to do it with a loop:> k <- i # just to allocate k > for (j in 1:3) {k[j] <- which(d[,1]==i[j]))} > print(d[k,2])But I believe there has to be a more efficient way in R without explicit loops, which I don?t know by now ??? Tia. Thomas -- ------------------------------------------------------- Dr. Thomas Bruns <thomas.bruns at ptb.de> Physikalisch-Technische Bundesanstalt Braunschweig Fachlabor: 1.13 Bundesallee 100 Tel: 0531/592 - 1132 D-38116 Braunschweig Fax: 0531/592-69-1132 ------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._