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.

`foo <- 5 # int foo = 5 in Java`

`bar = 5 # equivalent to above`

`bee <- 6.0 # double bee = 6.0`

`baz <- "hello" # String baz = "hello" in Java`

`truth <- TRUE # boolean truth = true in Java`

`falsity <- FALSE # boolean falsity = false in Java`

*Note: The "backwards arrow" syntax is preferred to avoid the ambiguity that comes with equals (equality or assignment).*

Vectors

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

*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.*

**List<T>**### Creating a Vector in R

Creating a vector in R is as easy as invoking a function.

`c(1,2,3) # like new int[]{1,2,3}`

`c(1:3) # equivalent to above using a range`

*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).

`# add two vectors together`

`c(1,2,3) + c(4,5,6) # equals 5 7 9`

``

`# add a single value to a vector`

`c(1,2,3) + 2 # equals 3 4 5`

### 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.

`a <- c(1,3,3,7)`

`a[1] # equals 1`

`a[4] # equals 7`

## Matrices

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.

`myMatrix <- matrix(data=c(1,2,3,4), nrow=2, ncol=2) # new int[][]{{1,3},{2,4}} in Java`

``

`# myMatrix is a 2 X 2 matrix`

`# [,1] [,2]`

`# [1,] 1 3`

`# [2,] 2 4`

*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

`myMatrix1 <- cbind(c(1,2), c(3,4)) # column bind equivalent to matrix above`

`myMatrix2 <- rbind(c(1,3), c(2,4)) # row bind equivalent to matrix above`

### 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

`m1 <- cbind(1:2, 3:4)`

`m2 <- cbind(c(1,1), c(1,1))`

`m3 <- m1 + m2`

``

`# m3 is a 2 X 2 matrix`

`# [,1] [,2]`

`# [1,] 2 4`

`# [2,] 3 5`

### Accessing Values in Matrices

Using the matrix **m3** from our addition operation above

`m3[1,2] # 1st row, 2nd column = 4`

`m3[2,2] # 2nd row, 2nd column = 5`

## Control Flow

Syntactically, most of the control flow mechanisms in R are very similar, if not the same, as the ones in Java

### If/Else

`if (a==TRUE) {`

` # do something`

`} else if (b==TRUE) {`

` # do something else`

`} else {`

` # do another thing`

`}`

There's the inline ternary equivalent ( e.g. in Java **value = condition ? true : false**)

`a <- FALSE`

`foobar <- ifelse(test=a==TRUE, yes="abc", no="xyz") # foobar = "xyz"`

But it's also a bit more powerful than just a ternary operator as you can use it for element-wise comparisons

`a <- c(TRUE, FALSE, TRUE)`

`foobar <- ifelse(test=a==TRUE, yes="abc", no="xyz") # foobar = "abc" "xyz" "abc"`

### Loops

While loops are identical to Java's

`while(foo==TRUE) {`

` # do something`

`}`

And for-loops are pretty similar to iterating an array in Java except you iterate a vector (and the syntax is slightly more compact)

`numbers <- c(1:10)`

`for (i in numbers) {`

` print(numbers[i])`

`}`

## 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.

### Plotting Points

To plot points, you use the **plot(...)** function and pass in a vector of x-values and vector of y-values

`a <- c(1,10,2,5)`

`b <- c(10,1,4,6)`

`plot(a,b) # plots the points (1,10), (10,1), (2,4), and (5,6)`

**plot(...)** can also take a number of other parameters to customize the look and feel of your plot