##
The Basics R language Statistics

Algebra
R has all the functions of a normal calculator; here we‘ll quickly
look at how to use these functions. Try entering the following:

> 1 + 1

The output should look like this:
[1] 2
Don‘t worry about what the [1] in front of the answer means, for now, that
will be covered later, in Section 4.3 —

###
Matrices.

From now on the input
and output will be put right after each other, like so:

> 1 - 1
[1] 0
The operators available are + (addition), - (subtraction), / (division), *
(multiplication), and ^ (raise to a power).
Most of the time, however, you will not want to just have the calculation
appear in the console window, you will instead want to save the
calculation as a ―variable‖.

###
To save a calculation as a variable, simply
think up a variable name, and then use an equal sign to assign the calculation‘s
output to that variable. For example:

> variable1 = 1 + 1
Now, there will not be an output of ―2‖ after you hit enter because instead
of outputting the result, R has saved the result to ―variable1‖.
OK, now that we‘ve covered some background information and made sure
everything is installed correctly, let‘s get down to the nitty-gritty of actual programming.
This section will walk you through the nuts and bolts of using R. Remember
to look back to Section 1.3 —
10
Now, try typing the following:

> variable1
[1] 2
You get the output that you saw before, as the calculation was the exact
same, it was just saved to the variable.
Note: Some other manuals you read may assign calculations to variables as follows:

> variable1 <- 1 + 1
The “<-” operator is, for our purposes, exactly the same as the “=“ operator in R
except for one exception, which is covered in Section 9: Functions. I prefer to
use the “=“ operator simply because it is more intuitive for me and is a keystroke
less than “<-”.
Ok, the last main point to cover about algebra in R is the order of operations.
The order is exactly the same as the one you likely learned in high
school; that is, multiplication and division are done first in order from left
to right and then addition and subtraction are done, again from left to
right.

For example:

> 1 + 2 / 3 - 2 * 6.5
[1] -11.33333
The order of operations can be controlled by using parenthesis. Operations
are performed from the innermost parenthesis outwards, so in the
following example:

> 1 * (2 / (1 + 1))
[1] 1
The calculation (1 + 1) is performed first. Then, (2 / 2) is calculated, followed
by 1 * 1. Whereas if this operation were written without parenthesis
the output would be as follows:

> 1 * 2 / 1 + 1
[1] 3
With 1 * 2 calculated initially, followed by 2 / 1 and finally 2 + 1.
11
4.2 — Vectors
The next main task that you should get comfortable within R is
creating and manipulating vectors (the word ―vector‖ is always used in
the mathematical sense in this section, the data type vector will be covered
later). A vector is a string of numbers; sometimes vectors are sequential
numbers, other times vectors are random numbers — there is no
restriction on the type or amount of numbers that a vector can contain.
The simplest way to create a vector is the following command:
> vector1 = 1:9

> vector1
[1] 1 2 3 4 5 6 7 8 9
The colon ―:‖ operator creates a sequence of numbers from the left hand
value to the right hand value, i
12
[1] 1 2 3 4 5 6 7 8 9

> vector4 = 9:1

> vector4
[1] 9 8 7 6 5 4 3 2 1
> vector3 + vector4
[1] 10 10 10 10 10 10 10 10 10
The first element of this new vector was 9 + 1, the second 8 + 2, etc.
Now, you may be wondering what happens if the vectors are not
the same size, well, let‘s try:

> vector5 = 1:3

> vector5
[1] 1 2 3

> vector3
[1] 1 2 3 4 5 6 7 8 9

> vector3 + vector5
[1] 2 4 6 5 7 9 8 10 12

What happens, as you can see, is that R loops the smaller vector as
many times as necessary to add something to each element of the larger
vector. In this case the first element is 1 + 1, the second element is 2 + 2,
the third element is 3 + 3, but then the smaller vector loops back to its the first element to add to the fourth element of the larger vector, giving the
calculation 1 + 4 for the fourth element.
When the larger vector is not a multiple of the smaller vector the process is the same, except R will give a warning message informing you
that the larger vector is not an integer multiple, so not all elements of the
smaller vector were added to the larger vector an equal number of times.
Try this situation for yourself.
Multiplying, dividing, exponentiating, and subtracting vectors works in the
same, element-by-element fashion.
1