Skip to content
Christopher Paciorek edited this page Aug 27, 2014 · 1 revision

Matlab to R dictionary

For anyone who is more familiar with MATLAB than with R, this website lists the R equivalents of common MATLAB commands:

Extracting matched patterns

There are a couple ways to do this. One is to use the () and \1, \2 type syntax in gsub() to cleverly replace an entire line with just the stuff you're interested in.

Another is to make use of the output of gregexpr(), which tells where a match is found. You can do this by using the additional function regmatches().

E.g.,

    x <- c("A and B", "A, B and C", "A, B, C and D", "foobar")
    pattern <- \"\[\[:space:\]\]*(,|and)\[\[:space:\]\]\"
    m <- gregexpr(pattern, x)
    regmatches(x, m)

Combining Plots

To combine plots, use the par( ) function with the following two options:

  • mfrow = c(n, m): Create a matrix of n rows by m columns in which the plots are filled by row
  • mfcol = c(n, m): Create a matrix of n rows by m columns in which the plots are filled by column

e.g.

   w <- rnorm(100); x <- rnorm(100); y <- rnorm(100); z <- rnorm(100)
   par(mfrow=c(2,2)) # Create 2 x 2 matrix to fill 4 plots
   plot(w, x, main = "w vs x")
   plot(x, y, main = "x vs y")
   plot(y, z, main = "y vs z")
   plot(z, w, main = "z vs w")