[R] Extracting data from matrices
Uwe Ligges
ligges at statistik.uni-dortmund.de
Wed May 12 14:39:24 CEST 2004
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
More information about the R-help
mailing list