Combine M and R programming languages for open source statistics

Join the M revolution – M and R programming languages

MUMPS, M and R programming languages
Image by :

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

“'I feel and think much as you do, care about many of the things you care about, although most people do not care about them. You are not alone.” Kurt Vonnegut

Previous articles

Developers who take a first peek at the M language may get a quick impression that it is strange and alien. Here is the good news: If you have used R, or have friends who know R, then you are in good company and can learn M in a much shorter time. Moreover, you can combine M with R to get a powerful with an excellent statistical package.

All is good and well in the open source universe.

Enough talking, let's start typing.

Open two terminals. On the left is your M interpreter, and on your right is your R interpreter.




Start the interpreter



The prompt you will see



How to quit the interpreter



Ask for general help



Ask for specific help



Assigning a variable

SET A=10

SET X="Higgs"

A <- 10

X <- "Higgs"

Displaying a variable



Variables are case sensitive

SET A=12

SET a=15



A <-12




Lists assignment

SET k("name")="Mary", k("spouse")="John", k("father")="Peter", k("mother")="Anne"

k <- list(name="Mary", spouse="John", father="Peter", mother="Anne")

List element access

WRITE k("name")

WRITE k("spouse")

WRITE k("father")

WRITE k("mother")

SET idx="name"

WRITE k(idx)





idx <- "name"


Basic functions


QUIT x*x

sqr < - function(x) {

x*x }

Calling function



The code is alive

R has a built-in object-oriented model, while M has a flexible data model. Both of them enable you to express situations such as: An object has a set of data properties, and a set of behaviors, and both of them can be changed at run time.

For example, let's think of a car. Let's consider three basic properties of a car: make, model, and mileage. Let's assume that the make and model are kinds of “data” properties of the car, while the mileage is something to be computed out of experimental conditions, and that is probably changing with the way you drive the car.

Let's express this in R:

car <- list(make=”toyota”,model=”corolla”,mileage=5)

Now, given the flexibility that late-binding gives us in R, we can decide to change mileage from a number into a function:

car$mileage <- function(miles,gallons) { miles/gallons }

Finally, we can query information from the car:




Now let's do the equivalent in M:

            SET ^car(“make”)=”toyota”,^car(“model”)=”corolla”,^car(“mileage”)=5

Then, use the ZEDIT command to put the following code in a file called mileage.m:

            ZEDIT “mileage”

Then type:


             quit miles/gallons

Note that in the second line, the “quit” command is indented one space to the right. Save the file and go back to the interpreter. Then type:

            SET ^car(“mileage”)=”$$compute^mileage”

Finally, we query the information from the car:

            WRITE ^car(“make”)

            WRITE ^car(“model”)

            WRITE @^car(“mileage”)@(150,4))

Note that the “@” symbols surrounding the string “^car(“mileage”)” indicate that there is a level of indirection to be done by the interpreter. In other words, they tell the interpreter to look at the content of the field “mileage” in the array “^car” and replace it in this expression.

These examples illustrate that M--just like R--provides a context in which one can compose complex constructs and use them to model concepts of real world problems.

Now imagine: What happens when we combine one of the most powerful statistical open source languages with one of the most powerful NoSQL open source databases? 

We’ll talk about this next time.

Learn more

About the author

Luis Ibanez - Luis Ibáñez works as Senior Software Engineer at Google Inc in New York city. Opinions expressed in this site are his own.You can find him in github at: in twitter at: previously worked as a Technical Leader at Kitware Inc., and Director of Open... more about Luis Ibanez