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.

Web Searches for "Programing R" since 2004

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.

AخA
 
1
foo <- 5 # int foo = 5 in Java
2
bar = 5 # equivalent to above
3
bee <- 6.0 # double bee = 6.0
4
baz <- "hello" # String baz = "hello" in Java
5
truth <- TRUE # boolean truth = true in Java
6
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 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. 

2
 
1
c(1,2,3) # like new int[]{1,2,3}
2
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). 

x
 
1
# add two vectors together
2
c(1,2,3) + c(4,5,6) # equals 5 7 9
3
4
# add a single value to a vector
5
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.

3
 
1
a <- c(1,3,3,7)
2
a[1] # equals 1
3
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.

6
 
1
myMatrix <- matrix(data=c(1,2,3,4), nrow=2, ncol=2) # new int[][]{{1,3},{2,4}} in Java
2
3
# myMatrix is a 2 X 2 matrix
4
#         [,1] [,2]
5
#    [1,]  1     3
6
#    [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

2
 
1
myMatrix1 <- cbind(c(1,2), c(3,4)) # column bind equivalent to matrix above
2
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 

8
 
1
m1 <- cbind(1:2, 3:4)
2
m2 <- cbind(c(1,1), c(1,1))
3
m3 <- m1 + m2
4
5
# m3 is a 2 X 2 matrix
6
#         [,1] [,2]
7
#    [1,]  2     4
8
#    [2,]  3     5

Accessing Values in Matrices

Using the matrix m3 from our addition operation above

2
 
1
m3[1,2] # 1st row, 2nd column = 4
2
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

7
 
1
if (a==TRUE) {
2
  # do something
3
} else if (b==TRUE) {
4
  # do something else
5
} else {
6
  # do another thing
7
}

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

2
 
1
a <- FALSE
2
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

2
 
1
a <- c(TRUE, FALSE, TRUE)
2
foobar <- ifelse(test=a==TRUE, yes="abc", no="xyz") # foobar = "abc" "xyz" "abc"

Loops

While loops are identical to Java's

3
 
1
while(foo==TRUE) {
2
  # do something
3
}

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

4
 
1
numbers <- c(1:10)
2
for (i in numbers) {
3
  print(numbers[i])
4
}

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

3
 
1
a <- c(1,10,2,5)
2
b <- c(10,1,4,6)
3
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