In this lesson we learn how to calculate Mathematical expressions, as well as store its results in a R object. In this lesson, all R code is below the R Source button, while the output is hidden. To see it you will need to click on the green "R Output" button. At the bottom of the page, you will find a bar which allows you to change the theme of the webpage (changing colors and format) so it can easily adapt to your system and preferences. There you also find "Code highlighting" which changes how R code is displayed to you, and Toggle R code and Figures.

## 8. Common Mathematical Expressions

Let's start by using R as a simple calculator in the console directly. So, type in the top-right panel the following expression:

3 + 4

Now, how about other, still very simple, operations?

3 * 4

Division

3/4

Exponentiation

3^4

Square Root

sqrt(169)

Natural logarithm

log(10)

Common logarithm

log10(10)

Use round brackets to group operations so that they are carried out first

(100 + 2)/3

What would happen if the round brackets were omitted?

100 + 2/3

The argument of a function can contain arithmetic operations or mathematical expressions

sqrt(25 - 9)

Note that in R, you should only use parenthesis (or round brackets) but not square brackets for expressions in R.

sqrt(((((7 * 8 + 1)/3) * 1/2)^2) * 4)

Another way to ask for R to evaluate (calculate) an expression or piece of code is to select it, and click on the Run button on the right side of the first pane. Or, while selected, press Ctrl + Enter on your keyboard. Let's see if we can reproduce the following expression in the "Source" pane (top-left), if you do it correctly, you should find 0.2193828.
$$[ (2^3 - 1) - log(10) ] / (3 + \sqrt{7^3 - 4})$$

Lastly, there are some named objects that R provides you, like the constant π or Month names and letters.

pi
month.abb  # try: month.name
LETTERS  # try: letters

## 9. Objects in R

R is what is known as an "object-oriented" language. For example, you can give a name to a mathematical expression, which has the benefit (and effect) of storing it for later use. Indeed, R does not usually display the results of analyses you perform right away like in SPSS, SAS or Stata. Instead, performed analyses are usually stored in an object for later use. For this reason, when doing statistics in R, you will often find yourself naming and storing objects. The name you choose should consist of letters, numbers, and the ".", and it should not start with a number.

In order to create an object in R, you need to assign it to a name. Names can be assigned by using the arrow-like signs <- and -> as demonstrated in the exercise below. Which sign you use depends on whether you prefer putting the name first or last (it may be helpful to think of -> as "put into" and <- as "set to"). It is also very common to use the equal sign '='.

# You can store the results of mathematical expressions
x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100

In order to see the result, you run the name of the object

x

Store the number 0.0001 under the name "small.num" (i.e. put 0.0001 into small.num). Note that R does not produce any output.

small.num <- 0.001

Now retrieve its value by running the saved object

small.num

You can put the name first if you reverse the arrow (set big.num to 10 * 100). This is easier.

big.num <- 10 * 100
big.num

A third way this is done is by using the equal sign like this.

normal.num = sqrt(100 * (12^2))/30
Every object in R has a type - all the variables we created are termed scalar. Scalar variable is a single number variable. x is a scalar variable whose value is 103, small.num is a scalar variable whose value is 0.001 and so forth.