Syntax Notes
- Data is stored in “objects” via
<-command.
> apple <- 1
> apple
[1] 1The bracket shows the index of the first element in the line.
> banana <- 1:30
> banana
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30- Lines are commented via
#.
# This is a comment!- The colon operator (:) returns every integer between two integers.
> die <- 1:6
> die
[1] 1 2 3 4 5 6- R does not always follow the rules of matrix multiplication. Instead, R uses element-wise execution. When you manipulate a set of numbers, R will apply the same operation to each element in the set. So for example, when you run
die - 1, R subtracts one from each element ofdie.- When you use two or more vectors in an operation, R will line up the vectors and perform a sequence of individual operations. For example, when you run die * die, R lines up the two die vectors and then multiplies the first element of vector 1 by the first element of vector 2. It then multiplies the second element of vector 1 by the second element of vector 2, and so on, until every element has been multiplied. The result will be a new vector the same length as the first two.

- If you give R two vectors of unequal lengths, R will repeat the shorter vector until it is as long as the longer vector, and then do the math. If the length of the short vector does not divide evenly into the length of the long vector, R will return a warning message. This behavior is known as vector recycling, and it helps R do element-wise operations.

- You can do inner multiplication with the
%*%operator and outer multiplication with the%o%operator:
> die <- 1:6
> die %*% die
[,1]
[1,] 91
> die %o% die
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 2 4 6 8 10 12
[3,] 3 6 9 12 15 18
[4,] 4 8 12 16 20 24
[5,] 5 10 15 20 25 30
[6,] 6 12 18 24 30 36- Functions are called in form of
func(args).
> round(3.1415, digits = 2)
[1] 3.14- They are also defined as this:

Functions
ls(): Shows the object names that are already used.
> ls()
[1] "apple" "banana" "die"round(),factorial(),mean(): basic functionsargs(): Shows the arguments of a function.
> args(round)
function (x, digits = 0, ...)
NULLsample(): Takes two arguments: a vector namedxand a number namedsize.samplewill returnsizeelements from the vector. (without replacement by default)
> die <- 1:6
> sample(die, 2)
[1] 1 5
> sample(die, 2, replace = TRUE)
[1] 1 1