2.1 Basic syntax
2.1.1 Operators
We can do basic maths in R using the following symbols:
Addition:
+Subtraction:
-Multiplication:
*Division:
/Exponentiation:
^
## [1] 7
## [1] 3
## [1] 10
## [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.
## [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:
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
variableNameTo view what has been assigned to an object, you can simply write the variable name:
## [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):
## [1] 5.25
A special form of a numeric variable is an integer variable, which is used for whole numbers.
## [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:
## [1] "This is a string"
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.
## [1] TRUE