[R] finding dedicated indexes?
Setzer.Woodrow@epamail.epa.gov
Setzer.Woodrow at epamail.epa.gov
Tue Mar 20 17:58:49 CET 2001
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list