Check out the following example:
> 2 + 2
[1] 4
> 9 / 3
[1] 3
> 5 %% 2 # modulus operator (remainder of 5 divided by 2)
[1] 1
Anything that occurs after the octothorpe or pound sign, #, (or hash-tag for you young'uns), is ignored by the R interpreter. This is useful to document the code in natural language. These are called comments.
In a multi-operation arithmetic expression, R will follow the standard order of operations from math. In order to override this natural order, you have to use parentheses flanking the sub-expression that you'd like to be performed first:
> 3 + 2 - 10 ^ 2 # ^ is the exponent operator
[1] -95
> 3 + (2 - 10) ^ 2
[1] 67
In practice, almost all compound expressions are split up with intermediate values assigned to variables that, when used in future expressions, are just like substituting the variable with the value that was assigned to it. The (primary) assignment operator is <-:
> # assignments follow the form VARIABLE <- VALUE
> var <- 10
> var
[1] 10
> var ^ 2
[1] 100
> VAR / 2 # variable names are case-sensitive
Error: object 'VAR' not found
Notice that the first and second lines in the preceding code snippet didn't have an output to be displayed, so R just immediately asked for more input. This is because assignments don't have a return value. Their only job is to give a value to a variable or change the existing value of a variable. Generally, operations and functions on variables in R don't change the value of the variable. Instead, they return the result of the operation. If you want to change a variable to the result of an operation using that variable, you have to reassign that variable as follows:
> var # var is 10
[1] 10
> var ^ 2
[1] 100
> var # var is still 10
[1] 10
> var <- var ^ 2 # no return value
> var # var is now 100
[1] 100
Be aware that variable names may contain numbers, underscores, and periods; this is something that trips up a lot of people who are familiar with other programming languages that disallow using periods in variable names. The only further restrictions on variable names are that they must start with a letter (or a period and then a letter), and that it must not be one of the reserved words in R such as TRUE, Inf, and so on.
Although the arithmetic operators that we've seen thus far are functions in their own right, most functions in R take the form, function_name(value(s) supplied to the function). The values supplied to the function are called arguments of that function:
> cos(3.14159) # cosine function
[1] -1
> cos(pi) # pi is a constant that R provides
[1] -1
> acos(-1) # arccosine function
[1] 3.141593
> acos(cos(pi)) + 10
[1] 13.14159
> # functions can be used as arguments to other functions
If you paid attention in math class, you'll know that the cosine of pi is -1 and that arccosine is the inverse function of cosine.
There are hundreds of such useful functions defined in base R, only a handful of which we will see in this book. Two sections from now, we will be building our very own functions.
Before we move on from arithmetic, it will serve us well to visit some of the odd values that may result from certain operations:
> 1 / 0
[1] Inf
> 0 / 0
[1] NaN
It is common during practical usage of R to accidentally divide by zero. As you can see, this undefined operation yields an infinite value in R. Dividing zero by zero yields the value NaN, which stands for Not a Number.