x <- 10
y <- 3
7 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.
7.1 Mathematical operations
We use the assignment operator <-
to assign values to variables x
and y
:
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 log 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
[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.
7.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.
[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
7.3 Common statistical operations
First, letβs use the rnorm()
function to draw 200 numbers at random from a normal distribution:
x <- rnorm(200)
7.3.1 Descriptive statistics
mean, median, standard deviation, minimum, maximum, and range:
mean(x)
[1] -0.001390185
median(x)
[1] 0.01809366
sd(x)
[1] 0.9764229
min(x)
[1] -2.589832
max(x)
[1] 2.107437
range(x)
[1] -2.589832 2.107437
summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.58983 -0.63743 0.01809 -0.00139 0.74105 2.10744
7.3.2 Sampling
Rβs sample()
allows you to sample 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] 23 24 25 26 29
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] 29 26 24 22 23 28 21 30 25 27
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] 21 28 21 24 23 25 27 21 24 21 25 27 29 30 27 27 21 24 29 25 30 24 24 25 26
[26] 30 27 22 26 27 28 30 30 24 24 24 23 21 27 27 22 29 27 27 24 27 21 22 25 27
[51] 22 24 21 29 30 30 26 23 29 21 26 24 30 24 24 27 21 30 28 22 21 24 30 24 23
[76] 22 26 24 22 21 21 26 22 27 25 22 27 30 25 30 23 27 27 22 28 26 22 22 30 28
7.4 Practice
Use the interactive code block to practice basic math operations. Feel free to write your own code and run it:
Experiment with the code below to see the difference between replace = FALSE
and replace = TRUE
: