[R] Extracting data from matrices

Daniel Bebber danbebber at forestecology.co.uk
Wed May 12 14:09:03 CEST 2004


Dear R list

I have an m * n matrix P and a vector V of length n containing indices for
rows in P.
For each of the m columns I want to extract the value in the row specified
by V, and put these values into a new vector W of length n.
At present I am doing this with a for.... loop, but I imagine there is a faster
way that doesn’t involve loops.
If anyone knows the way I would be most grateful.

Below is the code I am using at present-

for (i in 1:n){
W[i]<-P[V[i],i]}

Many thanks,
Dan Bebber
____________________________
Department of Plant Sciences
University of Oxford
South Parks Road
Oxford OX1 3RB
UK
Tel. 01865 275060
Fax. 01865 275074




More information about the R-help mailing list