Skip to content

Question on the idiom: start <- coef; start[fit$pivot] <- coef

2 messages · Barnet Wagman, Brian Ripley

#
I wonder if someone could  explain how the following R idiom works (it's
used in
glm.fit).

    start <- coef
    start[fit$pivot] <- coef

coef is a vector of coefficients, set by  .Fortran("dqrls", ...).
fit$pivot is a vector of integer indexes (indicating how dqrls permuted
the columns
of x).  If coef has n elements, fit$pivot is a permutation of seq(1,5).

start[fit$pivot] is simple enough, but the assignment performs a futher
permuation
that I don't understand.  For example
[1] 100 400 200 300 500


I guess there must be a replacement function for [], but I haven't
been able to dig up any documentation on it (and I'm not quite sure
where
to look in the source code).   I'd like to understand what R does when a
vector
is assigned to another vector in this way.

Thanks,
(and thanks for all the other help I've gotten from member of the list
lately)

-------------------
Barnet Wagman

wagman at enteract.com
773-645-8369

1361 N. Hoyne
Chicago, IL 60622
--------------------


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Fri, 30 Apr 1999, Barnet Wagman wrote:

            
I wouldn't try looking in the source code for something as fundamental as
this. It is simple:

b[c(1,3,4,2,5)] <- a

replaces the first element specified on the lhs by the first specified on
the rhs, etc. So it means	

b[1] <- 100; b[3] <- 200; b[4] <- 300; b[2] <- 400; b[5] <- 500

and this permuting (or reversing a permutation) is the key idea.

Understanding indexing in S is one key to unlocking its power: section 2.8
of Venables & Ripley might be a good place to look for a full description.