2.6 Let’s get started with R

2.6.1 R as a calculator

2.6.1.1 Simple calculations

R can be used as a calculator. Try some of the following below:

1 + 2
## [1] 3
1+2*3
## [1] 7

Well wait a second! were you all expecting the result to be 7? how many expected the result to be 9?

Check the following:

(1+2)*3
## [1] 9
1+(2*3)
## [1] 7

So parenthesis are important! Always use these to tell R (and any other software) the order of operations. This is the order (remember PEMDAS):

  1. Parentheses
  2. Exponents
  3. Multiplication and Division (from left to right)
  4. Addition and Subtraction (from left to right)

2.6.1.2 Functions

There are many built-in functions in R to do some complicated mathematical calculations.

2.6.1.2.1 Basic functions

Run some of the following.

sqrt(3)
## [1] 1.732051
3^2
## [1] 9
log(3)
## [1] 1.098612
exp(3)
## [1] 20.08554
2.6.1.2.2 Creating variables

We can also create variables (aka temporary place holders).

x <- 2
y <- 5
b <- x*y
x
## [1] 2
y
## [1] 5
b
## [1] 10
b+log(y)*x^2
## [1] 16.43775

When you create a variable and assign to it a number (or characters), you can use it later on.

2.6.1.2.3 Sequences

We can also create sequences of numbers

seq(1, 10, 2)
## [1] 1 3 5 7 9
?seq
z <- 1:10

And we can do the following.. Can you explain what we have done here?

z2 <- z+1
z*z2
##  [1]   2   6  12  20  30  42  56  72  90 110

Up to you… Write some more complex maths here just for fun!

## Add below

2.6.2 Objects

2.6.2.1 Basic objects

Objects are related to variables (we created above), but can also be dataframes, and other things we create in R. All of these are stored in memory and are shown below (under environment). You can check the type of the “object” below in the list (look at “Type”) or by using class().

Let’s look at the variables we created so far.. We will create another one as well…

class(b)
## [1] "numeric"
class(x)
## [1] "numeric"
class(y)
## [1] "numeric"
class(z)
## [1] "integer"
class(z2)
## [1] "numeric"
a <- "test"

class(a)
## [1] "character"

When we do calculations in R, we need to make sure we use numeric/integer variables only.. Try some of the below. Uncomment the following ##x + two and run the code.

x+y
## [1] 7
two <- "2"
##x + two

Can you explain the error?

We have tried to add a number to a (character) string which is clearly impossible. To do the maths, we need to change the class using any of the following commands: as.character, as.integer, as.numeric, as.factor, e.g.:

two <- as.numeric(two)
x + two
## [1] 4

2.6.2.2 Other functions and objects

2.6.2.2.1 Some more calculations

We can create a vector of objects to do various things on.. We use the function c() and do various things on:

numbers <- c(1,4,5,12,55,13,45,38,77,836,543)
class(numbers)
## [1] "numeric"
mean(numbers)
## [1] 148.0909
sd(numbers)
## [1] 276.6375
median(numbers)
## [1] 38
min(numbers)
## [1] 1
max(numbers)
## [1] 836
range(numbers)
## [1]   1 836
sum(numbers)
## [1] 1629
2.6.2.2.2 Referring to a specific position

Sometimes we may want to refer to a specific position in the list of numbers we just created… Use the following:

numbers[2]
## [1] 4
numbers[3:5]
## [1]  5 12 55
numbers[-4]
##  [1]   1   4   5  55  13  45  38  77 836 543
numbers+numbers[6]
##  [1]  14  17  18  25  68  26  58  51  90 849 556

Can you explain what we have done in the last operation?