8  Exercises

8.1 Getting started

8.1.1 Exploring RStudio

1 . Find the keyboard shortcuts menu:

Tools > Keyboard shortcuts help

  1. Change the appearance to something different.

8.1.2 Installing packages and using functions

  1. Loading packages

What function loads a package that is already on your computer?

8.1.3 Creating folders

Assuming we have created a project called library-r:

  1. Create a folder called R and folder called outputs in your project folder

8.2 Palmer penguins

Load the Palmer Penguins library if it’s not already loaded.

8.2.1 Palmer penguins dataset

  1. Find out about the penguins dataset:
  • what is it?
  • and what data types does it contain?

8.2.2 Vectors and assignment

Code
cat("Type the assignment operator ", fitb('<-'))

Type the assignment operator

  1. Create a character vector of your name and assign it to an object called my_name

    Code
    my_name <- "Alistair"
  2. Pass my_name to cowsay as an argument. Chose whatever animal you wish

    Code
    say(my_name, by = "monkey")
  3. Create a sequence of numbers from 1 to 10 and assign it to an object called my_seq

    Code
    my_seq <- seq(1:10)

8.2.3 Data frames/tibble

  1. Make a character vector of three names
  2. Make a numeric vector of three numbers
  3. Make a factor vector of three fruit
  4. Combine into a data frame.
Code
# For example
char_vec <- c("James","Hannah","Matt")
num_vec <- c(5,1792,23)
factor_vec <- factor(x = c("oranges","apples","grapes"))

# Combine into data.frame, the names of the variables will
# come from the objects.
df <- data.frame(char_vec,
                 num_vec,
                 factor_vec)

8.3 dplyr

All these exercises use dplyr from the tidyverse, but in addition to reading the book, you may have to Google or ask an AI assistance for help with some of these exercises.

8.3.1 Importing and inspecting data

Exercise 1: Import the data contained in csv file to an object called books from: https://raw.githubusercontent.com/ab604/library-r/main/data/books-2024-04-30.csv

Tip

You can read data directly from paths to on-line datasets with read_csv() as you would for locally stored csv files.

Code
# Exercise 1: Import the data contained in csv file to an object called `books`
books <- read_csv("https://raw.githubusercontent.com/ab604/library-r/main/data/books-2024-04-30.csv")