Basic skills in R: Your first calculations with R
Creating and working with variables
Everything in R is an object. In other words, objects can be almost anything in R, ranging from a single number, character string, or a list of numbers to highly complex structures like the output of a plot or a summary of a statistical analysis. Understanding how to create and modify objects is key to understanding R.
A more formal definition of an object is that of an instance of a certain type or class that has certain properties. An object in R can contain other objects.
Examples
10
(integer object)
"A short sentence."
(character object)
c(1, 2, 3, 4)
(vector object)
Variable and assignment in R
A variable is a storage location labelled with a symbolic name, which contains some quantity of information often referred to as a value. This value is actually an R object. But unlike an object, a variable has no type with certain properties. The name 'variable' indicates that its content can vary. Note that a variable in R differs from the statistical and mathematical concept of a variable.
Variables are handy because you can use them to store information or perform calculations. It's like having different boxes to organise and work with your data efficiently.
To create variable you simply give it a name. You can then assign a value to this object using the assignment operator <-
. The assignment operator is a composite symbol comprised of a ‘less than’ symbol < and a hyphen - .
To view the current value of the variable you simply enter the name of the variable in the console or use the function print()
in an R script. Alternatively, you can inspect the Environment/History panel, because it keeps track of all variables defined in the current R session.
A variable can be removed from the workspace via the function rm()
.
Example
Imagine you have a variable called age. You can use this variable to store someone's age, like 15 or 25. Later, you can look inside the age container to see what age it holds, or you can change its value to a different age if needed, e.g. one year more. You can also express the age in a word and print it in uppercase. The interactive session below illustrates this.
> age <- 15 > age [1] 15 > age <- age + 1 > age [1] 16 > age <- "sixteen" > toupper(age) [1] "SIXTEEN"
Doing calculations with variables
Working with variables The main reason for using R is that we leave it to the software to do tough calculations (including statistical tests) on the contents of our variables. So we don't have to do that by hand any more, as we did in the dark ages, and we can use names for object to make things more convenient. On the right you see sample script with indicated output. We can store the result of an calculation in a new variable. Like: Ask R to get you the contents of the Now run the instruction: Answers: the contents of your |
Sample script # doing some calculations with the "age" variable |
List of arithmetic operators The most common arithmetic operators for objects of type numeric are:
Operator | Name | Use | Result |
\(\red{\mathtt{\text{+}}}\) | plus | \(\mathtt{\text{x}}\)\(\mathtt{\text{+}}\) \(\mathtt{\text{y}}\) |
the sum of \(\mathtt{\text{x}}\) and \(\mathtt{\text{y}}\) |
\(\red{\mathtt{\text{-}}}\) | minus | \(\mathtt{\text{x}}\) \(\mathtt{\text{-}}\) \(\mathtt{\text{y}}\) | \(\mathtt{\text{x}}\) minus \(\mathtt{\text{y}}\) |
\(\red{\mathtt{\text{*}}}\) | times | \(\mathtt{\text{x}}\) \(\mathtt{\text{*}}\) \(\mathtt{\text{y}}\) | \(\mathtt{\text{x}}\) multiplied by \(\mathtt{\text{y}}\) |
\(\red{\mathtt{\text{/}}}\) | divided by | \(\mathtt{\text{x}}\) \(\mathtt{\text{/}}\) \(\mathtt{\text{y}}\) | \(\mathtt{\text{x}}\) divided by \(\mathtt{\text{y}}\) |
\(\red{\mathtt{\text{^}}}\) of \(\red{\mathtt{\text{**}}}\) | raised to the power | \(\mathtt{\text{x}}\) \(\mathtt{\text{**}}\) \(\mathtt{\text{y}}\) | \(\mathtt{\text{x}}\) to the power \(\mathtt{\text{y}}\) |
Less common, but still useful arithmetic operators are:
Operator | Name | Use | Result |
\(\red{\mathtt{\text{%%}}}\) | modulo | \(\mathtt{\text{x}}\) \(\mathtt{\text{%}}\) \(\mathtt{\text{y}}\) | the remainder of \(\mathtt{\text{x}}\) floor divided by \(\mathtt{\text{y}}\) |
\(\red{\mathtt{\text{%/%}}}\) | floor division | \(\mathtt{\text{x}}\) \(\mathtt{\text{%/%}}\) \(\mathtt{\text{y}}\) | The result of \(\mathtt{\text{x}}\) divided by \(\mathtt{\text{y}}\) rounded down (floored) to an integer |