[R] R is GNU S, not C.... [was "how to get or store ....."]
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Dec 6 21:34:54 CET 2005
On 12/6/05, Ted Harding <Ted.Harding at nessie.mcc.ac.uk> wrote:
> On 06-Dec-05 Martin Maechler wrote:
> >
> > [But really, I'm more concerned and quite bit disappointed by
> > the diehard ";" lovers]
> >
> > Martin Maechler
>
> Well, while not die-hard, I will put in my own little reason
> for often using ";" at the end of lines which don't need them.
>
> Basically, this is done to protect me from myself (so in fact
> is quite a strong reason).
>
> I tend to develop extended R code in a "side-window", using
> a text editor (vim) in that window, and cut&pasting the
> chunks of R code from that window into the R window.
> This usually means that I have a lot of short lines,
> since it is easier when developing code to work with the
> commands one per line, as they are easier to find and
> less likely to be "corrected" erroneously.
>
> Finally, when when I am content that the code does the job
> I then put several short lines into one longer one.
>
> For example (a function to do with sampling with probability
> proportional to weights); first, as written line-by-line:
>
> myfunction <- function(X,n1,n2,n3,WTS){
> N1<-n1;
> N2<-n1+n2;
> N3<-n1+n2+n3;
> # first selection
> pii<-WTS/sum(WTS);
> alpha<-N2;
> Pi<-alpha*pii;
> r<-runif(N3);
> ix<-sort(which(r<=Pi));
> # second selection
> ix0<-(1:N3);
> ix3<-ix0[-ix];
> ix20<-ix0[ix];
> W<-WTS[ix];
> pii<-W/sum(W);
> Pi<-N1*pii;
> r<-runif(length(Pi));
> ix10<-sort(which(r<=Pi));
> ix1<-ix20[ix10];
> ix2<-ix20[-ix10];
> # return the results
> list(X1=X[ix1],X2=X[ix2],X3=X[ix3],ix1=ix1,ix2=ix2,ix3=ix3)
> }
>
>
> Having got that function right, with 'vim' in command mode
> successive lines are readily brought up to the current line
> by simply pressing "J", which is very fast. This, in the
> above case, then results in
>
> MARselect<-function(X,n1,n2,n3,WTS){
> N1<-n1; N2<-n1+n2; N3<-n1+n2+n3;
> # first selection
> pii<-WTS/sum(WTS); alpha<-N2; Pi<-alpha*pii;
> r<-runif(N3); ix<-sort(which(r<=Pi));
> # second selection
> ix0<-(1:N3); ix3<-ix0[-ix]; ix20<-ix0[ix];
> W<-WTS[ix]; pii<-W/sum(W); Pi<-N1*pii;
> r<-runif(length(Pi)); ix10<-sort(which(r<=Pi));
> ix1<-ix20[ix10]; ix2<-ix20[-ix10];
> # return the results
> list(X1=X[ix1],X2=X[ix2],X3=X[ix3],ix1=ix1,ix2=ix2,ix3=ix3)
> }
>
> The greater readability of the first relative to the second is
> obvious. The compactness of the second relative to the first
> is evident. Obtaining the second from the first by repeated "J"
> is very quick.
I'm curious: exactly what purpose does this 'compactness' serve? The
file size doesn't decrease, since you are replacing newlines by
semicolons. It does not improve readability. So why do it at all?
-Deepayan
More information about the R-help
mailing list