---
title: Essential R Skills Self-Test
author: University of Glasgow, Department of Psychology
---
## Loading a Library
Almost every script you write will need to load a library. Do this at the top of the script. In the code chunk below, load the `tidyverse` and `readxl` libraries. You can check if you have a library under the Packages tab in the lower right RStudio pane. You might need to use `install.packages("tidyverse")` to install these libraries on your computer, but **never** put `install.packages()` in a script (just type that command directly into the console window).
```r
library(tidyverse)
library(readxl)
```
## Assigning Variables and Data Types
The main data types you will use are *numeric* (*integer* or *double*), *character*, and *boolean*.
You assign data to variables with the *assignment operator* `<-`. Assign the following variables:
* set the variable `four` to the integer 4
* set the variable `one_plus_3` to the sum of the integer 1 and the integer 3
* set `double_four` to the number four as a *double*
* set the variable `lower` to your first name in all lowercase letters
* set the variable `UPPER` to your first name in all uppercase letters
```r
four <- 4L
one_plus_3 <- 1L + 3L
double_four <- 4
lower <- "lisa"
UPPER <- "LISA"
```
## Comparison
The *comparison* operators, such as `==`, `>`, `<`, `>=`, and `<=` let you compare data and/or variables. Remember, `=` *sets* the left side of the equation equal to the right side, while `==` checks if the left and right sides are *equivalent*.
If you assign a variable to the outcome of a comparison, such as `is_equal <- 10 == 5 + 5`, the variable will be TRUE if the comparison is true and FALSE if the comparison is false. Assign the following variables to the outcome of the specified comparison:
* `comp_1_eq_2`: is the integer 1 equal to the integer 2?
* `comp_a_eq_A`: is the character "a" equal to the character "A"?
* `comp_10_lt_20`: is the number 10 less than the number 20?
* `comp_four_gte_one_plus_3` is the variable `four` greater than or equal to the variable `one_plus_3`
```r
comp_1_eq_2 <- 1L == 2L
comp_a_eq_A <- "a" == "A"
comp_10_lt_20 <- 10 < 20
comp_four_gte_one_plus_3 <- four >= one_plus_3
```
## Forming a vector
A vector is a list of 1 or more items of the same data type. Create the following variables:
* `beatles`: a list of first names John, Paul, George, and Ringo
* `one_to_ten`: a list of the integers 1 through 10
* `evens`: a list of the even integers from 2 to 10
* `a_to_e`: the first 5 items in the built-in vector `letters`
```r
beatles <- c("John", "Paul", "George", "Ringo")
one_to_ten <- 1:10
evens <- c(2, 4, 6, 8, 10) # seq(2, 10, by = 2)
a_to_e <- letters[1:5]
```
## Vectorised Operations
* `by_tens`: multiply each item in the vector `one_to_ten` by 10
* `squares`: make a 10-item vector with the square of the numbers 1 to 10 (multiply each number by itself)
* `a_to_e_1`: use the `paste0` function and the `a_to_e` variable you created above to create a vector that looks like `"a1", "b2", "c3", "d4", "e5"`.
```r
by_tens <- one_to_ten * 10
squares <- 1:10 * 1:10 # (1:10)^2
a_to_e_1 <- paste0(a_to_e, 1:5)
```
## Loading Data
You can load data straight from a website using the URL. Use tidyverse functions for reading CSV and Excel files to create the following variables:
* `infmort`: load data in the CSV file "https://psyteachr.github.io/msc-data-skills/data/infmort.csv" using the URL
* `matmort`: download the excel file [matmort.xls](https://psyteachr.github.io/msc-data-skills/data/matmort.xls) into a directory called `data` in the same directory as this file and load the local file using a function from the package `readxl`
```r
infmort <- read_csv("https://psyteachr.github.io/msc-data-skills/data/infmort.csv")
```
```
## Parsed with column specification:
## cols(
## Country = col_character(),
## Year = col_double(),
## `Infant mortality rate (probability of dying between birth and age 1 per 1000 live births)` = col_character()
## )
```
```r
matmort <- read_xls("data/matmort.xls")
```
## Checks
The sections below let you check your work. There is no need to change anything. Knit this file to HTML. If you did everything right, all the answers should be appear as `correct` in the HTML report.
The following sections let you check your work. If you did everything right, all the answers should be TRUE.
### Assigning Variables and Data Types
* `four`: correct
* `one_plus_3`: correct
* `double_four`: correct
* `lower`: correct
* `UPPER`: correct
### Comparison
* `comp_1_eq_2`: correct
* `comp_a_eq_A`: correct
* `comp_10_lt_20`: correct
* `comp_four_gte_one_plus_3`: correct
### Forming a vector
* `beatles`: correct
* `one_to_ten`: correct
* `evens`: correct
* `a_to_e`: correct
### Vectorised Operations
* `by_tens`: correct
* `squares`: correct
* `a_to_e_1`: correct
### Loading Data
* `infmort`: correct
* `matmort`: correct