[R] Re: Matlab to R ?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Fri Oct 4 14:31:56 CEST 2002

On 04-Oct-02 Martin Maechler wrote:
>     Robin> This shows how common Matlab idioms translate to R.
> and I assume you have seen Paul Gilbert's reply, giving "ex"
> translations of a few idioms.
>     Robin> There seem to be more and more people interested in
>     Robin> porting from Matlab/octave to R.  How about a
>     Robin> specialist mailing list?
> Probably everyone agrees that a Matlab -> R translation cannot
> (and probably should not) be automated to full extent.  On the
> other hand, Paul Gilbert's "ex" script should be made into a
> perl or python {or "octave" !?} script and could be enhanced
> considerably, probably.

While matlab/octave are a powerful packages for numerical and
logical computation on vectors and matrices, and therefore lend
themselves well to Statistics (I was using octave and, earlier,
MatLab long before R was properly on the scene), they not only
differ from R in their primary purposes but also in their languages
and constructs. For this reason I doubt that Paul Gilbert's
'ex' script will encourage many to migrate from matlab/octave
to R: simple line-editing substitutions do no go far enough;
the script is not "intelligent".

To take a simple example: I have an ocyave file "sap.m" which
simply reads in some data and constructs some derived data:

  SAP = [ 112 108 110 85 92 NaN 89 NaN 112 NaN NaN
  107 NaN 115 NaN 104 NaN NaN NaN NaN NaN NaN
  67 81 107 109 110 100 NaN NaN NaN NaN NaN ];
  SAP20  = SAP(:,1);
  SAP30  = SAP(:,2);
  SAP120 = SAP(:,11);

Paul's script produces:
  SAP <- [ 112 108 110 85 92 NaN 89 NaN 112 NaN NaN
  107 NaN 115 NaN 104 NaN NaN NaN NaN NaN NaN
  67 81 107 109 110 100 NaN NaN NaN NaN NaN ]
  SAP20  = SAP(:,1)
  SAP30  = SAP(:,2)
  SAP120 = SAP(:,11)

and all it has done is to replace "=" by "<-" and remove the
semicolons. It has not recognised the data structure (a matrix)
being assigned to SAP. 'R' will not accept this output, even at
its firs tline, let alone get as far as doing the wrong thing
with it! Nor will the subsidiary assignments at the end work.

In fact, it is not obvious (except perhaps to a fluent "sight
reader" of 'ex' commands) what the script does, what it does not
do, and  -- above all -- what it does that it should have done

I would agree that a more flexible language such as 'perl' or
'python' or (my favourite) 'awk' should be used instead: these
languages are capable of sophisticated parsing of input lines,
recognising their intent, and generating appropriate output.
I.e. the programmer can build "intelligence" into the script.

But in fact I think I would prefer to see a general interface
whereby R and octave/matlab could cooperate with each other,
each calling the other as required and contributing their
respective strengths, rather than join in a movement to induce
users to migrate from octave/matlab to R. To some extent this
can be done without effort, since R can import and export
octave/matlab files in the "save -ascii" format (though I have
observed that for import to work it is necessary to edit out
the first line of the octave file). Matlab/octave can be readily
programmed to generate command files for another language (I have
often programmed 'pic' commands from within octave, for instance,
to generate complex data plots from internal data), and I dare
say one could do the same from within R (though I have never
tried this myself).

> It could help matlab/octave users switch to R.
> Another reason I'm interested to some extent is the
> Statistics+Matlab community & software ("Wavelets", "Smoothers" etc)
> where I would like us to get better connected to.

See above.

> Robin> How about a specialist mailing list?
> Not sure if this is needed and makes sense;

I'm inclined to agree with this: experience shows that the
subscriber mass is usually sub-critical! The best place to
catch the eye of people who might be interested is the main list.

Best wishes to all,

E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 04-Oct-02                                       Time: 13:31:56
------------------------------ XFMail ------------------------------
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

More information about the R-help mailing list