23+13
[1] 36
4*5+7
[1] 27
4*(5+7)
[1] 48
4*(5+7)^2
[1] 576
Open R Sessions 2024
Etka Yapar
Iain Moodie
Simon Jacobsen Ellerstrand
Violeta Caballero-Lopez
Ximena Alva Caballero
The exercises in this session are intended for you to start to become familiar with R and Rstudio. Do not worry if everything has not “clicked” by the end. You will gain more confidence the more time you spend using these tools.
To install R, follow these steps:
.pkg
file link to download the installer for the latest version of R.For most Linux distributions, you can install R from the command line.
To install RStudio, follow these steps:
For most Linux distributions, you can install RStudio from the command line.
Once you have downloaded both R and RStudio, open RStudio. Your installation of R should be automatically detected, and you will be greated with an interface that looks like this:
Rstudio is designed around a four panel layout. Currently you can see three of them. To reveal the fourth, go to File -> New file -> R script. Now your window should look like this:
Ctrl+Enter
(Windows) or Cmd+Enter (macOS). You can also send the whole script to the console, using the Source button, or by using Ctrl+Shift+S
(Windows) or Cmd+Shift+S
(macOS).There are some good habits to get into early when you start programming. Most of these apply to any programming language. They help you keep your work organised, and make sure it is reproducible.
We recommend you create a folder where you save all the work you do as part of the Open R Sessions. Within that folder, you should create a subfolder for each session. An example might be a folder called open_r
that within it contains folders open_r/01_intro
, open_r/02_datatypes
, etc. Create a folder and subfolder for this session.
We now want to set our working directory to this folder. A working directory is the directory (folder) in a file system where a user is currently working. It is the default location where commands, scripts, and programs are stored and executed and where files are read from or written to unless specified otherwise. To set the working directory using RStudio, go to Session -> Set working directory -> Choose directory, then navigate to the folder you just made for this session.
You should notice that doing this, the command setwd(your_directory)
was sent to the console. Most buttons in RStudio will simply run an R command, that you could also write.
To create a new script (if you have closed the one you made before), go to File -> New file -> R script. Now save this script into your folder for this session, by going to File -> Save. Give the file a name that makes sense to you. R script files generally end with the extension .R
or .r
.
It is possible to write comments in an R script that are not read by the computer. To do this, you start a line with a #
. You can use this feature to write notes to yourself as you go, but you can also use it to give your script a title, so you remember what it does, or what it was for. For example, I might include at the start of my script:
Add something similar to your script.
Another common practise is to include the command rm(list=ls())
at the start of an R scripts. This command will clear the R environment of all variables. It makes sure that your script will always run the same no matter who runs it. Add this line to the top of your script now.
Another good practise is to declare your working directory at the start of a script. You can do this using the setwd()
we encountered before.
Remember to save your script!
You are now setup to do some coding!
You will often want to use R to analyse some of your data. The first step is naturally to import it into R. On the canvas page, you can download a dataset called “Simple_data.txt”. Download it to the folder you set as a working folder. Most of you are used to working with data in Excel files and not text files. Don’t worry! We will talk more about file types in the coming sessions. For now, you want to import the file with read.delim()
. First, write the command list.files()
into your script, and run it. This lists all the files in your working directory. Does the .txt
files appear? If so, then write and run data <- read.delim("Simple_data.txt", head=T, sep="\t")
.
The data file has now been imported to R and stored in the object data
. An easy way of getting an overview of the data is by looking at the top 5 rows with the command head(data)
. To see the whole file, you can also open it by clicking on the object which has now appeared in the Global Environment. As you can see, it contains four columns with information on some of Simon’s samples of lark species. Return to the script by clicking on the correct tab. To get some basic summary statistics of the datafile you can try a few other commands.
Try str(data)
, table(data$Species)
, table(data$Sex)
, and table(data$Species, data$Sex)
.
Now, try on your own to make a table containing the number of individuals of each population and species.
Hopefully, this will give you a very fast idea of some of the content of this data. More on importing files and the str()
command in the coming sessions!
One of the many ways of using R is simply to use it as a calculator. You just need to write the formula you desire to calculate and R will do it immediately. Note that R will always follow the mathematical calculation priorities. These priorities can be overruled with the use of parenthesis. Some mathematical operators used in R are the following: +
, -
, *
, /
, ^
. Here are some examples:
Do you get the same answers?
In R, a variable is a name assigned to a value or a data object, which can be used to store data for later use. Variables in R can hold various types of data, including numbers, strings, vectors, lists, and more. We cover this in the next session. Variables are created using the assignment operator <-
.
Notice that once you declare a variable, it appears in the environment tab.
Let’s focus on the variable we just made called x
. We can now use x
in other commands. For example, we could find the square root of x
:
Notice that if we change what x
is, we get a different answer:
The order your commands come in matters.
Previous members of the Open R Team have coded some games in R! To try them out, go to the canvas page, and follow the instructions in the games_in_r.pdf
file.
---
title: "Introduction to R Exercises"
subtitle: "Open R Sessions 2024"
author:
- Etka Yapar
- Iain Moodie
- Simon Jacobsen Ellerstrand
- Violeta Caballero-Lopez
- Ximena Alva Caballero
format:
html:
code-fold: true
code-tools: true
code-overflow: wrap
toc: true
toc-depth: 4
self-contained: true
anchor-sections: true
smooth-scroll: true
theme:
light: flatly
dark: darkly
number-sections: true
number-depth: 3
execute:
echo: true
warning: false
eval: false
---
The exercises in this session are intended for you to start to become familiar with R and Rstudio. Do not worry if everything has not "clicked" by the end. You will gain more confidence the more time you spend using these tools.
# Installing R and RStudio
## R
To install R, follow these steps:
### Windows
1. Go to the [CRAN R Project website](https://cran.r-project.org/).
2. Click on the "Download R for Windows" link.
3. Click on the "base" link to download the base system.
4. Click on the "Download R x.x.x for Windows" link (where x.x.x is the latest version).
5. Once the download is complete, open the installer and follow the on-screen instructions to complete the installation.
### macOS
1. Go to the [CRAN R Project website](https://cran.r-project.org/).
2. Click on the "Download R for macOS" link.
3. Click on the `.pkg` file link to download the installer for the latest version of R.
4. Once the download is complete, open the installer and follow the on-screen instructions to complete the installation.
### Linux
For most Linux distributions, you can install R from the command line.
## RStudio
To install RStudio, follow these steps:
1. Go to the [RStudio website](https://posit.co/download/rstudio-desktop/).
### Windows
1. Click on the "Download RStudio Desktop for Windows" link.
2. Once the download is complete, open the installer and follow the on-screen instructions to complete the installation.
### macOS
1. Click on the "Download RStudio Desktop for macOS" link.
2. Once the download is complete, open the installer and follow the on-screen instructions to complete the installation.
### Linux
For most Linux distributions, you can install RStudio from the command line.
# Getting familiar with RStudio
Once you have downloaded both R and RStudio, open RStudio. Your installation of R should be automatically detected, and you will be greated with an interface that looks like this:

Rstudio is designed around a four panel layout. Currently you can see three of them. To reveal the fourth, go to *File* -> *New file* -> *R script*. Now your window should look like this:

1. **Script**: a script is simply a text file, where you write and save commands. It is designed to be read like English, left to right and top to bottom. The order of commands matters! The most common way to work with R is to write your commands in a script, and then send those commands to the console. Saving your script allows you to come back to a project later, or send it to someone else.
2. **Console**: the console is where you write or send commands to be executed by the computer. Usually, you have written your commands in the script, and send each line of commands to the console using either the *Run* button, or by using `Ctrl+Enter` (Windows) or Cmd+Enter (macOS). You can also send the whole script to the console, using the *Source* button, or by using `Ctrl+Shift+S` (Windows) or `Cmd+Shift+S` (macOS).
3. **Environment**: this panel shows you objects loaded into R. We will come back to this later.
4. **Viewer**: this panel has many functions, but is commonly used to show the output of your commands (e.g. plots), or to read the R help documentation.
# Before you start coding
There are some good habits to get into early when you start programming. Most of these apply to any programming language. They help you keep your work organised, and make sure it is reproducible.
## Working directory
We recommend you create a folder where you save all the work you do as part of the Open R Sessions. Within that folder, you should create a subfolder for each session. An example might be a folder called `open_r` that within it contains folders `open_r/01_intro`, `open_r/02_datatypes`, etc. Create a folder and subfolder for this session.
We now want to set our working directory to this folder. A working directory is the directory (folder) in a file system where a user is currently working. It is the default location where commands, scripts, and programs are stored and executed and where files are read from or written to unless specified otherwise. To set the working directory using RStudio, go to *Session* -> *Set working directory* -> *Choose directory*, then navigate to the folder you just made for this session.
You should notice that doing this, the command `setwd(your_directory)` was sent to the console. Most buttons in RStudio will simply run an R command, that you could also write.
## Creating a script
To create a new script (if you have closed the one you made before), go to *File* -> *New file* -> *R script*. Now save this script into your folder for this session, by going to *File* -> *Save*. Give the file a name that makes sense to you. R script files generally end with the extension `.R` or `.r`.
## Some good practises
It is possible to write comments in an R script that are not read by the computer. To do this, you start a line with a `#`. You can use this feature to write notes to yourself as you go, but you can also use it to give your script a title, so you remember what it does, or what it was for. For example, I might include at the start of my script:
```r
# Open R Sessions 01
# 2024-09-19
```
Add something similar to your script.
Another common practise is to include the command `rm(list=ls())` at the start of an R scripts. This command will clear the R environment of all variables. It makes sure that your script will always run the same no matter who runs it. Add this line to the top of your script now.
```r
# Open R Sessions 01
# 2024-09-19
rm(list=ls())
```
Another good practise is to declare your working directory at the start of a script. You can do this using the `setwd()` we encountered before.
```r
# Open R Sessions 01
# 2024-09-19
rm(list=ls())
setwd(write your directory here)
```
Remember to save your script!
You are now setup to do some coding!
# Exercises
## Importing data to R
You will often want to use R to analyse some of your data. The first step is naturally to import it into R. On the canvas page, you can download a dataset called “Simple_data.txt”. Download it to the folder you set as a working folder. Most of you are used to working with data in Excel files and not text files. Don’t worry! We will talk more about file types in the coming sessions. For now, you want to import the file with `read.delim()`. First, write the command `list.files()` into your script, and run it. This lists all the files in your working directory. Does the `.txt` files appear? If so, then write and run `data <- read.delim("Simple_data.txt", head=T, sep="\t")`.
The data file has now been imported to R and stored in the object `data`. An easy way of getting an overview of the data is by looking at the top 5 rows with the command `head(data)`. To see the whole file, you can also open it by clicking on the object which has now appeared in the Global Environment. As you can see, it contains four columns with information on some of Simon's samples of lark species. Return to the script by clicking on the correct tab. To get some basic summary statistics of the datafile you can try a few other commands.
Try `str(data)`, `table(data$Species)`, `table(data$Sex)`, and `table(data$Species, data$Sex)`.
Now, try on your own to make a table containing the number of individuals of each population and species.
Hopefully, this will give you a very fast idea of some of the content of this data. More on importing files and the `str()` command in the coming sessions!
## R as a very powerful calculator
One of the many ways of using R is simply to use it as a calculator. You just need to write the formula you desire to calculate and R will do it immediately. Note that R will always follow the mathematical calculation priorities. These priorities can be overruled with the use of parenthesis. Some mathematical operators used in R are the following: `+`, `-`, `*`, `/`, `^`. Here are some examples:
```{r}
#| eval: true
#| code-fold: false
23+13
4*5+7
4*(5+7)
4*(5+7)^2
```
Do you get the same answers?
## Creating variables in R
In R, a variable is a name assigned to a value or a data object, which can be used to store data for later use. Variables in R can hold various types of data, including numbers, strings, vectors, lists, and more. We cover this in the next session. Variables are created using the assignment operator `<-`.
```{r}
#| code-fold: false
#| eval: true
# Assigning a numeric value to a variable
x <- 10
# Assigning a string value to a variable
course <- "Open R Sessions"
# Assigning a vector to a variable
numbers <- c(1, 2, 3, 4, 5)
# Assigning a list to a variable
person <- list(name = "Alice", age = 30, occupation = "Engineer")
```
Notice that once you declare a variable, it appears in the environment tab.
Let's focus on the variable we just made called `x`. We can now use `x` in other commands. For example, we could find the square root of `x`:
```{r}
#| code-fold: false
#| eval: true
sqrt(x)
```
Notice that if we change what `x` is, we get a different answer:
```{r}
#| code-fold: false
#| eval: true
x <- 42
sqrt(x)
```
The order your commands come in matters.
# Games
Previous members of the Open R Team have coded some games in R! To try them out, go to the canvas page, and follow the instructions in the `games_in_r.pdf` file.