# 3 First Operations

In this lesson we learn how to calculate Mathematical expressions, as well as store its results in a R object.

## 3.1 Common Mathematical Expressions

Let’s start by using R as a simple calculator in the console directly. So, if I want to add 3 to 4, I type in the console panel the following expression: `3 + 4`

and press Enter. You should see as a result the following:

`## [1] 7`

Now, how about other mathematical operations?

Multiplication: how much is 3 times 4?

`## [1] 12`

Division: how much is 3 divided by 4?

`## [1] 0.75`

We are doing great! Now let’s see how we can interactively learn **directly** from this web materials.

Let’s take an easy example. How much is `7 + 8`

? You probably know the answer, but let’s check below. **The box will turn green when you typed the right answer.. And as long as the answer is incorrect, the box remains purple.**

- The sum of 7 and 8 is
- How much is 3 times 3 times 3?
- What is the remainder in the division between 10 and 3

**Great! Let’s learn more R!**

Exponentiation: how much is 3 to the 4th power?

`## [1] 81`

Square Root: what is squared root of 169?

`## [1] 13`

Natural logarithm: what is the natural logarithm of 10?

`## [1] 2.302585`

Common logarithm: what is the logarithm based 10 of 10?

`## [1] 1`

You can use round brackets `(`

and `)`

to group operations so that they are carried out first.

`## [1] 34`

But what would happen if the round brackets were omitted? The result would NOT be the same.

The argument of a function is *what goes between round brackets inside a function.* Here, `sqrt(25)`

inside the function `sqrt()`

the number 25 is the argument. It can also contain arithmetic operations or mathematical expressions such as `sqrt(25-9)`

`## [1] 4`

Note that in R, you should only use parenthesis (or round brackets) but not square brackets for expressions in R. For example `sqrt(((((7*8 + 1)/3) * 1/2)^2)*4)`

. Type that in the console, and check the result.

`## [1] 19`

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})\]

`try a step by step approach, and consider separating expressions with round brackets`

## 3.2 Miscellaneous

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

As you learn R, you will see it has many easy functions aimed at facilitating difficult tasks (in other programming languages and statistical softwares).

`pi`

`## [1] 3.141593`

Months

```
## [1] "January" "February" "March" "April" "May"
## [6] "June" "July" "August" "September" "October"
## [11] "November" "December"
```

Months abbreviated

```
## [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov"
## [12] "Dec"
```

Letters - all caps

```
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q"
## [18] "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
```

letters

```
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
```