Intro

The purpose of this assignment is to allow you to practically implement the concepts introduced during class. Although some of the content builds on the class discussion and examples, the exercises go beyond the scope of only the material covered in class. You are encouraged to consult external sources to solve the challenges in this document.

Question 1

What are the shortcut commands for the following:

  • inserting the assignment operator (<-)
  • inserting the pipe operator (%>%)
  • focus on the (a) Source Editor, (b) Console, (c) Help panes respectively
  • manually accessing the suggestion list
  • run the current line of code and jump to the next line
  • run current line of code without jumping to the next line
  • run the entire script
  • run the script from the beginning to the current line
  • toggling between tabs
  • editing with multiple cursors
  • moving a line of code in the script
  • copying a line of code
  • deleting a line of code

ANSWER 1

  • Alt+ -
  • Ctrl+Shift+m
  • Ctrl+1, Ctrl+2, Ctrl+3 or Ctrl+Alt+F1
  • Alt+Shift+K
  • Ctrl+Enter
  • Alt+Enter
  • Ctrl + Alt + R
  • Ctrl + Alt + B/E
  • Ctrl+Shift+Tab & Ctrl + F11/ F12 & Ctrl + F9/F10
  • Ctrl + Alt + (Up/Down)
  • Alt + Up/Down
  • Shift+Alt+(Up/Down)
  • Ctrl+D

Question 2

  1. Read in the data from “data/worldcup_top.csv” and save it as worldcup_top.
library(tidyverse)
worldcup_top <- read_csv("~/project/Markdown_Training/Homework/data/worldcup_top.csv")
  1. Read in the data from “data/worldcup_bottom.csv” and save it as worldcup_bottom. Why do we need to take a different approach than we did in (a)?
#worldcup_bottom <- rio::import("~/project/Markdown_Training/Homework/data/worldcup_bottom.csv") %>% janitor::clean_names()
worldcup_bottom <- read_delim("~/project/Markdown_Training/Homework/data/worldcup_bottom.csv",delim = "|",
                              show_col_types = FALSE)

colnames(worldcup_bottom) <- gsub("(.*)[0-9]+?_", "", colnames(worldcup_bottom))
  1. Use the pipe operator to select the year and goals_scored column from worldcup_top and worldcup_bottom respectively. What is different about worldcup_bottom, how do you address this issue?
#selecting some columns from worldcup_top df
worldcup_top_piped <- worldcup_top %>% 
                  select(year,goals_scored)
#selecting some columns from worldcup_bottom_df
worldcup_bottom_piped <- worldcup_bottom%>% 
                  select(year,goals_scored)

The difference between df are follow: (a) worldcup_top is like head of data set which is starting from year \(1930\) and other is like tail of the entire data which is started from \(1982\).

Question 3

The code chunk below creates a list called shops_list that you also saw in the class material. Extract from the list using only [] and [[]] indexing:

  1. the data frame that indicates the stock in shop 1
  2. the names of the two staff members working at shop 1
# code to create shops_list

stock_data <- data.frame(
  crops = c("maize", "soya", "rice", "potatoes"),
  quantity_ordered = c(100, 200, 38, 1050),
  price_per_kg = c(1000, 1855.99, 99.50, 500),
  in_stock = c(TRUE, TRUE, FALSE, TRUE)
)

staff_data <-  data.frame(
  names = c("Jean de Dieu", "Martha"),
  monday = c(TRUE, TRUE),
  tuesday = c(TRUE, TRUE),
  wednesday = c(FALSE, TRUE),
  thursday = c(FALSE, TRUE),
  friday = c(TRUE, TRUE)
)

shop_1_list <- list(
  active = TRUE,
  stock = stock_data,
  staff = staff_data
)

shop_2_list <- list(
  active = FALSE,
  stock = stock_data,
  staff = staff_data
)

shops_list <- list(
  shop_1 = shop_1_list,
  shop_2 = shop_2_list 
)

# Answer the questions here:

# (a)
shop_1_list[[2]]
##      crops quantity_ordered price_per_kg in_stock
## 1    maize              100      1000.00     TRUE
## 2     soya              200      1855.99     TRUE
## 3     rice               38        99.50    FALSE
## 4 potatoes             1050       500.00     TRUE
# (b)
shop_1_list$staff[1]
##          names
## 1 Jean de Dieu
## 2       Martha