Barnet Wagman
1999-Apr-30 07:36 UTC
[R] Question on the idiom: start <- coef; start[fit$pivot] <- coef
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
> pivot <- c(1,3,4,2,5)
> a <- c(100,200,300,400,500)
> b <- a
> b[pivot] <- a
> b
[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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Prof Brian D Ripley
1999-Apr-30 08:17 UTC
[R] Question on the idiom: start <- coef; start[fit$pivot] <- coef
On Fri, 30 Apr 1999, Barnet Wagman wrote:> 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 > > > pivot <- c(1,3,4,2,5) > > a <- c(100,200,300,400,500) > > b <- a > > b[pivot] <- a > > b > [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.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. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._