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
Dear Dan,
you could just use the following:
V <- cbind(V, 1:n)
W <- P[V]
I hope this helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Doctoral Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Daniel Bebber" <danbebber at forestecology.co.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, May 12, 2004 2:09 PM
Subject: [R] Extracting data from matrices
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
Daniel Bebber wrote:> 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]}So, nrow(P)=n and ncol(P)=m ? What happens if you have less columns than rows to be selected? Anyway: W <- P[V + nrow(P) * (seq(along = V) - 1)] Uwe Ligges> Many thanks, > Dan Bebber > ____________________________ > Department of Plant Sciences > University of Oxford > South Parks Road > Oxford OX1 3RB > UK > Tel. 01865 275060 > Fax. 01865 275074 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html