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

From: Dave Roberts <droberts_at_montana.edu>
Date: Thu 08 Dec 2005 - 03:12:41 EST

Well, this has been an interesting thread. I guess my own perspective is warped, having never been a C programmer. My native languages are FORTRAN, python, and R, all of which accept (or demand) a linefeed as a terminator, rather than a semicolon, and two of which are very particular about whitespace.

Accepting for a moment Ted's argument about wanting to compact his code, the problem as I understand it is that ";" is a statement separator, not a statement terminator, so that Ted really does need all those ";" in between his statements on a single line, but that the last one implies a NULL statement on every line. Given that the ";" facilitates compacting lines in vi (or vim), how about when the code is compacted you try

1,$s/;$//

which will remove all the final trailing ";" leaving the other necessary ";" separators.

Dave R.

(Ted Harding) 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.
>
> 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@nessie.mcc.ac.uk>
> Fax-to-email: +44 (0)870 094 0861
> Date: 06-Dec-05 Time: 19:02:23
> ------------------------------ XFMail ------------------------------
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts                                     office 406-994-4548
Professor and Head                                      FAX 406-994-3190
Department of Ecology                         email droberts@montana.edu
Montana State University
Bozeman, MT 59717-3460

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Dec 08 03:30:43 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:34 EST