R and Octave/Matlab notes
As the Debian r-base package states: “S is the statistician’s Matlab and R is to S what Octave is to Matlab”. These notes document translations between R and Octave, and sometimes Matlab. (I do not have S and do not have access to Matlab all the time.)
Notes elsewhere
See the much more comprehensive notes on CRAN and on mathesaurus. This page just has a couple of things I have noted after working them out.
Data transfer
R uses the nice platform-independent XDR binary format for saving its workspace. I'm sure one could read this into Octave with a bit of effort. However, is probably easier to use HDF5, which is not the native format of either R and Octave, but is supported out of the box by both. In R do:
library(hdf5) hdf5save("filename.hdf","var1","var2","var3",...)
Then in octave
load "filename.hdf"
works for simple data types, including matrices. Matlab should work too (it
has a function called hdf5read
), but I haven’t tried.
Sourcing a script file
In Octave/Matlab typing the name of a .m
-file runs the commands as
if typed into the current workspace. This has the same effect as
source("filename.r",local=T,print.eval=T)
in R.
Timing
Wall clock time
Octave tic; ...; time_taken=toc R times=system.time((...)) time_taken=times[3]
CPU time
Octave start_time=cputime(); ...; cpu_time_taken=cputime()-start_time R times=system.time((...)) cpu_time_taken=sum(times[-3])
To record start and stop times in R, call proc.time
before and after
the code. The difference between these vectors is what system.time
returns.
The performance of R and Octave should be in the same ball-park for many things, although I have not done a careful comparison.