Simple Codes

Arithmetic and Storing Values

Ben Dickins

Expression

There are three types of expression in R
1. Numbers: 1, 2, 0.2, -5, etc
2. Strings: alphabets or anything that is input by “” into R
3. Logical: TRUE/FALSE

Expression

  • Numbers:
2+3
  • Strings:
"Hello"
  • TRUE/FALSE
3<4
2+4 == 4

Arithmetic Operators

x + y sum
x - y subtract
x * y multiply
x / y divide
x ^ y power

Arithmetic Operators

Do it yourself:

2+4
[1] 6
2^3
[1] 8
850/10
[1] 85
220-20
[1] 200

Storing Values

  • In all programming languages we can store values in variables and access them later.
  • This can be done in various ways using a selection of assignment operators.
  • The most commonly used assignment operator is <-, see the example below:
x <- 3
y <- "Hey!"

Note: In most contexts the = operator can be used as an alternative.