From Octave to R

GNU-Octave is a program not unlike Matlab.

Help

Octave R
help -i help.start()
help help(help)
help sort help(sort)
demo()
(Matlab) lookfor plot apropos(‘plot’) or help.search(‘plot’)

Assignment

There are multiple assignment operators in R.

Octave R
a=1; a <- 1
a=1 a <- 1; a or (a <- 1)

Complex Numbers

Octave R Comments
3+4i 3+4i
i 1i R treats “i” as a variable name
abs(3+4i) Mod(3+4i) but put a numeral in front
arg(3+4i) Arg(3+4i)
conj(3+4i) Conj(3+4i) get these from help(Mod)
real(3+4i) Re(3+4i)
imag(3+4i) Im(3+4i)
sqrt(-2) sqrt(-2+0i) R has explicit complex arithmetic

Vectors

Vectors and Sequences

Octave R
1:10 1:10 or seq(10)
1:3:10 seq(1,10,by=3)
10:-1:1 10:1
10:-3:1 seq(from=10,to=1,by= -3)
linspace(1,10,7) seq(1,10,length=7)
(1:10)+i 1:10+1i
a(1:end) a[1:length(a)]

Vectors and Concatenation

Octave R comments
a=[2 3 4 5]; a <- c(2,3,4,5) R output not echoed to the screen if assigned
a=[2 3 4 5] (a <- c(2,3,4,5))(but round brackets around an expression force echo)
adash=[2 3 4 5]’ ; adash <- t(c(2,3,4,5))
[a a] c(a,a)
[a a*3] c(a,a*3)
a.*a a*a
a.^3 a^3
[1:4 a] c(1:4,a)

Repeating Vectors

Octave R
[1:4 1:4] rep(1:4,2)
repmat(1:4,1,3) rep(1:4,3)
repmat(1:4,3,1) kronecker(matrix(1,3,1),t(1:4))
nonzeros(triu(repmat(1:4,4,1)))’ rep(1:4,1:4)
repmat(1:4,3,1)(:)’ rep(1:4,each=3)

Negative indexing

In R, the negative indexing means that the elements are not wanted.

Octave R
a=1:100; a <- 1:100
a(2:100) a[-1]omit the first element.
a([1:9 11:100]) a[-10]omit the tenth element.
a([1:9 12:100]) a[-(10:11)]need parentheses: “:” operator has low priority
a([find(mod(0:49,3)), 51:end]) a[-seq(1,50,3)]omit 1,4,7,...

Destructive modification:

Octave R
a(1) = [] a=a[-1]remove the first element.
a(10) = [] a=a[-10]remove the tenth element.
a(1:3:50) = [] a=a[-seq(1,50,3)]remove 1,4,7,...

Boolean indexing

Octave R
(1:3)[true false true] (1:3)[c(TRUE,FALSE,TRUE)]
a(a>90)= -44; a[a>90] <- -44
find(a>90) which(a>90)

Max and Min

Octave R
a=randn(1,4); a <- rnorm(4)
b=randn(1,4); b <- rnorm(4)
max(a,b) pmax(a,b) mnemonic: pairwise max.
max([a’ b’]) cbind(max(a),max(b))
max([a b]) max(a,b)
[m i] = max(a) m <- max(a) ; i <- which.max(a)

Ranks

Octave R
ranks(rnorm(8,1)) rank(rnorm(8))
ranks(rnorm(randn(5,6))) apply(matrix(rnorm(30),6),2,rank)

Matrices

Binding

Octave R
[1:4 ; 1:4] rbind(1:4,1:4)
[1:4 ; 1:4]’ cbind(1:4,1:4) or t(rbind(1:4,1:4))
[2 3 4 5] c(2,3,4,5)
[2 3;4 5] rbind(c(2,3),c(4,5)) rbind() binds rows
[2 3;4 5]’ cbind(c(2,3),c(4,5)) or matrix(2:5,2,2)
a=[5 6]; a <- c(5,6)
b=[a a;a a]; b <- rbind(c(a,a),c(a,a)) see repmat below
[1:3 1:3 1:3 ; 1:9] rbind(1:3, 1:9)
[1:3 1:3 1:3 ; 1:9]’ cbind(1:3, 1:9)
[repmat(1:3,1,8)(1:8);1:8]rbind(1:3, 1:8)

Other

Matlab R Comments
tic tic= function() {.first.time «- proc.time()}
toc toc= function() {proc.time()- .first.time} then use tic(); command; toc(); as usual

Debugging

Matlab R Comments
keyboard browser() ; debug(“function_name”) to exit debugging mode see below
return Q Halt execution and drop to top level see Here, chapter 9 (Debugging)
ans .Last.value
disp(44) print(44)

Plotting

Matlab R Comments
a=rand(10) a <- array(runif(100),c(10,10)) or matrix(runif(100),nrow=10)
help plot help (plot) and methods(plot)
plot(a) matplot(a,type=”l”,lty=1)
plot(a,’r’) matplot(a,type=”l”,lty=1,col=”red”)
plot(a,’x’) matplot(a,pch=4) see example(“points”) for the symbols
plot(a,’–‘) matplot(a,type=”l”,lty=2)
plot(a,’x-’) matplot(a,pch=4,type=”b”,lty=1)
plot(a,’x–‘) matplot(a,pch=4,type=”b”,lty=2)
semilogy(a) matplot(a,type=”l”,lty=1,log=”y”)
semilogx(a) matplot(a,type=”l”,lty=1,log=”x”)
loglog(a) matplot(a,type=”l”,lty=1,log=”xy”)
plot(1:10,’r’), hold on, plot(10:-1:1,’b’) plot(1:10,col=”red”,type=”l”); matplot(10:1,col=”blue”,type=”l”,add=TRUE) or matlines(10:1,col=”blue”)
plot([1:10 10:-1:1]), axis equal ? not sure ? maybe MASS::eqscplot(1:10,10:1)

TODO to be continued

Based on a reference sheet translating between the most common Octave (or Matlab) and R commands provided by Robin Hankin.

Also see Dave Hiebeler’s extensive reference guide

Romain François 2006/03/04

Kath 2009/06/10

 
getting-started\translations\octave2r.txt · Last modified: 2009/09/01
 
Recent changes RSS feed R Wiki powered by Driven by DokuWiki and optimized for Firefox Creative Commons License