[R] R is GNU S, not C.... [was "how to get or store ....."]

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Tue Dec 6 20:02:26 CET 2005


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.

BUT -- if I had not put the ";" at the ends of the lines in the
string-out version (which is easy to do as you type in the line
in the first place), then it would be much more trouble to get
the second version, and very easy to get it wrong!

Also, being long used to programming in C and octave/matlab,
putting ";" at the end of a command is an easy reflex, and of
course does no harm at all to an R command.

Not that I'm trying to encourage others to do the same as I
do -- as I said, it's a self-protective habit -- but equally
if people (e.g. me) may find it useful I don't think it should
be discouraged either -- especially on "aesthetic" grounds!

Just my little bit ...

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 06-Dec-05                                       Time: 19:02:23
------------------------------ XFMail ------------------------------




More information about the R-help mailing list