[R] finding dedicated indexes?

Thomas Bruns Thomas.Bruns at ptb.de
Tue Mar 20 16:57:37 CET 2001


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list