2.1 Basic syntax

2.1.1 Operators

We can do basic maths in R using the following symbols:

  • Addition: +

  • Subtraction: -

  • Multiplication: *

  • Division: /

  • Exponentiation: ^

# Addition and subtraction
5 + 2
## [1] 7
6 - 3
## [1] 3
# Multiplication and division
2 * 5
## [1] 10
24/6
## [1] 4

R will recognise brackets and perform calculations appropriately, following BEDMAS (or PEMDAS). As you normally would with a written equation, R will perform calculations left to right.

(6 + 2) * 3/4
## [1] 6

2.1.2 Assignment and naming

When you type something in R, you almost always have the option of assigning that value to an object/variable to give it a name. In R, assigning values/strings etc. to objects is slightly different to other programming languages. Instead of using equals signs, we use a left arrow <- for assignment. For example:

# Use this
x <- 5

# This works but isn't preferred 
x = 5

When it comes to naming variables and the like, the easiest/most readable way for most people is to separate words using an underscore. e.g.

# this is preferred
variable_name

# this is also very common
variable.name

# sentence case is also sometimes used
VariableName

# no spaces are allowed
variable name # This will give you an error, as R will think this is two separate variables

# some heathens use camel case
variableName

To view what has been assigned to an object, you can simply write the variable name:

# This is the variable we named earlier
x
## [1] 5

In general, it is good practice to use variable names that are clear but concise. In other words, avoid naming your variables something like x1, x2 etc. Instead, use the name of the measure/thing the data represents directly, e.g. scale_total.

2.1.3 Variable types

Like many programming languages, R works by manipulating different types of variables. Knowing how to work with these different variables is fairly essential to using R, so here is a brief overview.

First, we have our most basic classes of variables. The first is numeric, which is as it says on the tin (i.e. it stores numbers):

var_a <- 5.25
var_a
## [1] 5.25

A special form of a numeric variable is an integer variable, which is used for whole numbers.

var_b <- 6
var_b
## [1] 6

The second is character, which is used for text (named strings in programming language). Strings in character variables must be enclosed with speech marks:

var_c <- "This is a string"
var_c
## [1] "This is a string"
# "This" and "is" would be treated by R as two separate strings

Finally, we have logical variables, which can take on the form of TRUE or FALSE. TRUE and FALSE (or alternatively T or F) are special values in R that, as their names suggest, are used to indicate when a certain value is true or false.

var_d <- TRUE
var_d
## [1] TRUE