6  Basic operations

First, before even learning about data types and structures, it may be worth looking at some of the basic mathematical and statistical operations in R.

6.1 Mathematical operations

We use the assignment operator <- to assign values to variables x and y:

x <- 10
y <- 3

Standard arithmetic operations are as expected:

x + y
[1] 13
x - y
[1] 7
x * y
[1] 30
x / y
[1] 3.333333

Exponentiation uses ^: (The caret (^) is likely the most common but not the only symbol used for exponentiation across programming languages)

x^3
[1] 1000

Square root is sqrt():

sqrt(81)
[1] 9

Natural logs with log():

log(12)
[1] 2.484907

Base 10 log with log10():

log10(1000)
[1] 3

Exponential function with exp():

exp(1)
[1] 2.718282

Integer division i.e. Divide and forget the remainder

x %/% y
[1] 3

i.e. how many times the denominator fits in the numerator, without taking fractions of the denominator. It can be applied on decimals the same way:

9.5 %/% 3.1
[1] 3

Modulo operation i.e. Divide and return just the remainder

x %% y
[1] 1
x <- (-10:10)[-11]
y <- sample((-10:10)[-11], size = 20)
x - (x %/% y) * y == x %% y
 [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE

Trigonometric functions

cos(x)
 [1] -0.8390715 -0.9111303 -0.1455000  0.7539023  0.9601703  0.2836622
 [7] -0.6536436 -0.9899925 -0.4161468  0.5403023  0.5403023 -0.4161468
[13] -0.9899925 -0.6536436  0.2836622  0.9601703  0.7539023 -0.1455000
[19] -0.9111303 -0.8390715
sin(x)
 [1]  0.5440211 -0.4121185 -0.9893582 -0.6569866  0.2794155  0.9589243
 [7]  0.7568025 -0.1411200 -0.9092974 -0.8414710  0.8414710  0.9092974
[13]  0.1411200 -0.7568025 -0.9589243 -0.2794155  0.6569866  0.9893582
[19]  0.4121185 -0.5440211
tan(x)
 [1] -0.6483608  0.4523157  6.7997115 -0.8714480  0.2910062  3.3805150
 [7] -1.1578213  0.1425465  2.1850399 -1.5574077  1.5574077 -2.1850399
[13] -0.1425465  1.1578213 -3.3805150 -0.2910062  0.8714480 -6.7997115
[19] -0.4523157  0.6483608

See ?cos for more specialized trigonometric functions and details.

6.2 Logical operations

Logical AND: &

TRUE & TRUE
[1] TRUE
TRUE & FALSE
[1] FALSE

Logical OR: |

TRUE | FALSE
[1] TRUE

Logical negation: !

x <- TRUE
!x
[1] FALSE

Exclusive OR: xor()
XOR evaluates to TRUE when two logicals are different,
i.e. one or the other is TRUE but not both.

a <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE)
b <- c(FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)
a & b
[1] FALSE FALSE  TRUE FALSE FALSE FALSE
a | b
[1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
xor(a, b)
[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE

Test all elements of an object are TRUE with all():

all(a)
[1] FALSE

Test if any element is TRUE with any():

any(a)
[1] TRUE

6.3 Common statistical operations

First, let’s use the rnorm() function to draw 200 numbers at random from a normal distribution:

x <- rnorm(200)

6.3.1 Descriptive statistics

mean, median, standard deviation, minimum, maximum, and range:

mean(x)
[1] -0.02076856
[1] 0.0274838
sd(x)
[1] 0.9661436
min(x)
[1] -3.054364
max(x)
[1] 2.507989
[1] -3.054364  2.507989
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
-3.05436 -0.66635  0.02748 -0.02077  0.56176  2.50799 

6.3.2 Sampling

R’s sample() allows you to samples elements of an R object with or without replacement:

By default, the replace argument is set to FALSE, i.e. sampling is performed without replacement and you can request a sample size up to the length of the object:

x <- 21:30
sample(x, size = 5)
[1] 29 22 25 26 24

size is the second argument and its name can therefore be omitted if it is the second value you pass to sample():

sample(x, 10)
 [1] 22 23 26 28 21 25 30 24 27 29

Setting replace = TRUE performs sampling with replacement and you can set size to any positive integer, including values larger than the length of the input:

sample(x, size = 100, replace = TRUE)
  [1] 30 29 23 30 24 24 23 30 23 24 24 22 22 22 29 28 22 23 21 29 27 25 24 22 24
 [26] 23 26 25 30 27 27 29 24 30 25 28 25 30 26 30 29 25 26 30 22 22 25 27 22 27
 [51] 21 29 26 30 28 29 29 23 24 22 26 30 22 26 21 28 25 27 25 27 29 22 22 25 27
 [76] 30 25 21 26 27 22 25 26 24 24 30 30 28 30 28 22 27 28 22 27 27 28 24 28 30