As a Software Engineer I'm always keeping up to date with technology and one of the things that helps me do that is book reviews. Right now, I'm reviewing The Book of R: A First Course in Programming and Statistics by Tilman M. Davies.
R is a programming language used in statistical computing and has gained in popularity in the recent years due to the explosion of data and its applications like Machine Learning.
This is my first introduction to R and one of the ways I find helpful in learning in a new language is to learn (or at least compare) it in terms of another language. As of Sept 2016, the Tiobe index still rates Java as the most popular language so I thought it would be useful to explain the syntax of R in terms of Java.
Variables and Types
Defining a variable in R is straightforward. You simply name the variable, followed by a "backwards arrow" (or an equals sign) and the value. No need to worry about defining the type like you would in Java.
Although you don't specify the type, R has many of the common types you'd come to expect from a language like ints, doubles, strings, booleans, etc.
Note: The "backwards arrow" syntax is preferred to avoid the ambiguity that comes with equals (equality or assignment).
Vectors are a key component in R. Don't confuse the terminology though; these are not the vectors that you use in math that have a direction and magnitude. The term vector, here, means a collection of similarly-typed things and is most comparable to an Array with values of all-the-same type or List<T> in Java. Being a language for statistical computing, it makes sense that vectors are a key component of R since they can be used as a container for your dataset that you're analyzing.
Creating a Vector in R
Creating a vector in R is as easy as invoking a function.
Note: The 'c' in c(...) stands for combine
Math with Vectors
R is a "vector oriented" language where as Java is a "scalar oriented" language. What that means is you can perform the most basic operations like addition, subtraction, multiplication, etc on vectors just like you do in Java on scalars (e.g. 2 + 2).
Accessing Values in a Vector
You can access values in vector just like you access values in an Array in Java. The only exception is that vectors in R are 1-indexed and not 0-indexed.
Where as the term, "vector" was used in a non-mathematical sense, matrices in R are indeed the mathematical construct you're familiar with. They're used in some parts of statistics so it only makes sense that R includes support for them. The equivalent Java structure would be a 2D Array.
Creating a Matrix in R
You create a matrix by invoking the matrix(...) function, passing in a vector as the data and specifying the number of rows and columns.
Note: nrow = number of rows, ncol = number of columns.
Notice how the data is filled in the matrix? By default, the supplied vector is filled in a columnar fashion. You also have the option of filling the matrix in a row-wise fashion by supplying the optional argument byRow=TRUE. And if you're lazy, there's shortcut functions you can use instead
Math with Matrices
Just like vectors, you can perform the most basic operations like addition, subtraction, multiplication, etc on matrices. For example, two add two matrices
Accessing Values in Matrices
Using the matrix m3 from our addition operation above
Syntactically, most of the control flow mechanisms in R are very similar, if not the same, as the ones in Java
There's the inline ternary equivalent ( e.g. in Java value = condition ? true : false)
But it's also a bit more powerful than just a ternary operator as you can use it for element-wise comparisons
While loops are identical to Java's
And for-loops are pretty similar to iterating an array in Java except you iterate a vector (and the syntax is slightly more compact)
Plotting & Graphing
One of the areas where R is extremely useful and has no Java equivalent (at least built-in to Java), is the ability to plot and graph points.
To plot points, you use the plot(...) function and pass in a vector of x-values and vector of y-values
plot(...) can also take a number of other parameters to customize the look and feel of your plot