This is your worksheet β for explanations, analogies, and worked
examples, read lab_workbook.html first.
Come back here to actually do each module. Work through this over ~4
days, 1-2 modules per day.
Whenever you see <write your code here>, replace
it with real code. Whenever you see a question, answer it in a sentence
or two right below.
π Read Module 1 in the Workbook first.
Task 1.1: In the Console (not in a chunk), type
print("Hello, R!") and press Enter.
Task 1.2: Run the chunk below (green play button, or Cmd/Ctrl + Enter).
print("Hello, R!")
## [1] "Hello, R!"
Question 1.2.1: Whatβs the difference between typing code in the Console versus typing it in a chunk here?
Typing in the console allows you to run code immediately, but code typed in the source as a chunk is now a part of the doc that can be rerun and saved in the knitted report
Task 1.3: Check the Environment pane (should be empty). Run the chunk below, then check again.
my_first_variable <- 42
Question 1.3.1: What showed up in the Environment pane?
my_first_variable under values and 42 next to it
Task 1.4: Add a comment above the chunk in Task 1.2 explaining, in your own words, what that chunk does.
print("Hello, R!") #this gives the value of a simple string of text
## [1] "Hello, R!"
Task 1.5: Click Knit right now, before youβve filled in anything below. Confirm an HTML file opens showing this document so far, unfinished parts and all.
Task 1.6: In the Console (not in a chunk), run
console_only_variable <- 99. Now, in the chunk below,
write print(console_only_variable) and try knitting the
whole document.
Question 1.6.1: What happened when you knitted? Why, given what the Workbook said about fresh sessions?
Error:
! object 'console_only_variable' not found
Backtrace:
β
1. ββbase::print(console_only_variable)
Quitting from lab_0.Rmd:77-79 [unnamed-chunk-4]
Execution halted
When you knit before typing in the source it is saved, but when you knit after only typing in the console it is not going to store that information.
Task 1.7: Comment out the line you just wrote in Task 1.6, then knit again to confirm the document succeeds cleanly.
π Read Module 2 in the Workbook first.
cups_of_coffee <- 3
Task 2.1: Create three variables: one numeric, one character, one logical.
brownies_baked <-10
bakery_name <- "something sweet"
orders_received <- TRUE
Task 2.2: Use class() to check the type
of each variable you just made.
class(brownies_baked)
## [1] "numeric"
class(bakery_name)
## [1] "character"
class(orders_received)
## [1] "logical"
cups_yesterday <- 5
cups_today <- 8
total_cups <- cups_yesterday + cups_today
total_cups
## [1] 13
Task 2.4: Reassign cups_today to be 3
more than its current value (same βvariable equals itself plus
somethingβ pattern above). Print it to confirm.
cups_yesterday <- 5
cups_today <- 8
cups_today <- cups_today + 3
cups_today
## [1] 11
Question 2.4.1: What happens if you add a numeric
and a character variable, like cups_today + "hello"? Try it
in the Console (not here β it would break the knit!) and describe what R
tells you.
Error in cups_today + "hello" : non-numeric argument to binary operator
Trying to add a text value and a numeric value is nonsensical
π Read Module 3 in the Workbook first.
daily_sales <- c(12, 18, 9, 22, 15)
daily_sales
## [1] 12 18 9 22 15
Task 3.1: Create a vector of at least 5 numbers of something you like counting.
vegetables_sold <- c(2, 7, 19, 4, 22)
vegetables_sold
## [1] 2 7 19 4 22
daily_sales[1]
## [1] 12
daily_sales[3]
## [1] 9
daily_sales[-1]
## [1] 18 9 22 15
Task 3.2: Pull the 2nd and 4th values from your Task 3.1 vector. Then, separately, pull everything except the last value.
vegetables_sold[2]
## [1] 7
vegetables_sold[4]
## [1] 4
vegetables_sold[-length(vegetables_sold)]
## [1] 2 7 19 4
vegetables_sold * 2
## [1] 4 14 38 8 44
vegetables_sold + 10
## [1] 12 17 29 14 32
Task 3.3: Create a new vector that adds 5 to every
value in your Task 3.1 vector. Then check how many items are in your
original vector with length().
vegetables_sold_plus_5 <- vegetables_sold + 5
vegetables_sold_plus_5
## [1] 7 12 24 9 27
length(vegetables_sold)
## [1] 5
Question 3.3.1: What do you think happens if you
combine a number and text in one vector, like
c(1, "two", 3)? Try it β what type does R make the whole
vector?
the whole vector now becomes text, or character values
π Read Module 4 in the Workbook first.
sum(daily_sales)
## [1] 76
mean(daily_sales)
## [1] 15.2
max(daily_sales)
## [1] 22
min(daily_sales)
## [1] 9
sort(daily_sales)
## [1] 9 12 15 18 22
round(mean(daily_sales), 1)
## [1] 15.2
Task 4.1: Find the sum, mean, and max of your Task 3.1 vector.
sum(vegetables_sold)
## [1] 54
mean(vegetables_sold)
## [1] 10.8
max(vegetables_sold)
## [1] 22
min(vegetables_sold)
## [1] 2
sort(vegetables_sold)
## [1] 2 4 7 19 22
round(mean(vegetables_sold), 1)
## [1] 10.8
Task 4.2: Try sort() on your vector,
then separately try sort(your_vector, decreasing = TRUE).
Whatβs different?
sort(vegetables_sold)
## [1] 2 4 7 19 22
sort(vegetables_sold, decreasing = TRUE)
## [1] 22 19 7 4 2
Task 4.3: Look up the help page for
round() (?round in the Console). What does the
digits argument do with a negative number, like
-1? (Try round(1234, -1).)
the digits number indicates the number of decimal places (round) or significant digits (signif) to be used. Negative values are allowed.
π Read Module 5 in the Workbook first.
daily_sales > 15
## [1] FALSE TRUE FALSE TRUE FALSE
daily_sales > 10 & daily_sales < 20
## [1] TRUE TRUE FALSE FALSE TRUE
daily_sales[daily_sales > 15]
## [1] 18 22
Task 5.1: Keep only the values in your Task 3.1 vector that are greater than its own average.
vegetables_sold > 15
## [1] FALSE FALSE TRUE FALSE TRUE
vegetables_sold > 10 & vegetables_sold < 20
## [1] FALSE FALSE TRUE FALSE FALSE
vegetables_sold[vegetables_sold > 15]
## [1] 19 22
vegetables_sold[vegetables_sold > mean(vegetables_sold)]
## [1] 19 22
Task 5.2: Filter your vector to values between two
numbers of your choosing, using &.
vegetables_sold[vegetables_sold > 12 & vegetables_sold < 20]
## [1] 19
Task 5.3: Count how many values are above the
average, using sum() on a logical comparison.
sum(vegetables_sold > mean(vegetables_sold))
## [1] 2
Question 5.3.1: In plain words, what does
daily_sales[daily_sales > 15] actually do, step by
step?
Each value in my vector is compared to 15 (output is TRUE/FALSE) and then leaves behind all that are greater not greater than 15 (FALSE)
π Read Module 6 in the Workbook first.
# Run these once in the Console if you don't have them yet:
install.packages("readr")
install.packages("dplyr")
library(readr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Task 6.1: Run the chunk above. Red text mentioning
βmaskedβ objects is normal; an actual error means you need to run
install.packages() first.
Task 6.2: Run installed.packages()[, 1]
in the Console. Do you see readr and dplyr in
the list?
Yes
Question 6.2.1: Why do you think R separates βinstallβ (once) from βloadβ (every session)?
Loading a package with library() makes its functions available in your current session aftyer it is already installed on your computer
π Read Module 7 in the Workbook first.
getwd()
## [1] "/Users/vinishabrowne/Desktop/ENIA-5120/labs/lab0"
Task 7.1: Does the printed path match
ENIA-5120 itself, or somewhere inside it? (Hint: read the
Workbookβs note on why getwd() in the Console can differ
from where code runs inside this .Rmd.)
It is the absolute path starting from my computer to user to ENIA-5120 folder
example_path <- file.path("..", "..", "data", "hoya_grounds_sales.csv")
example_path
## [1] "../../data/hoya_grounds_sales.csv"
Task 7.2: lab_template.Rmd lives in
ENIA-5120/labs/lab0. Using file.path(), build
the relative path from here to a file called practice.csv
that lives directly in ENIA-5120/data β how many
.. do you need, and why?
ex_path <- file.path("..", "..", "data", "practice.csv")
ex_path
## [1] "../../data/practice.csv"
list.files()
## [1] "lab_0.html" "lab_0.Rmd" "lab_answers.Rmd"
## [4] "lab_workbook.html"
Task 7.3: Run list.files() above β what
folderβs contents does it show? Then try
list.files("../../data") and compare.
list.files("../../data")
## [1] "climate_stations.csv" "hoya_grounds_sales.csv"
Question 7.3.1: Whatβs the difference between an
absolute path and a relative path? Why does the working directory
inside an .Rmd file default to the
.Rmdβs own folder, rather than your R Projectβs root?
An absolute path gives lengthy directions from the root of the computer and always leads to the same file regardless of where R origin is (tied to specific user/name). A relative path gives directions from wherever R currently is β and inside an .Rmd, that's always the folder the .Rmd itself lives in, not the R project root, even though the Console's getwd() shows the Project root. This works the same on every
our classmates cpus long as everyone's project folder is set up the same way.
π Read Module 8 in the Workbook first.
names <- c("Alex", "Jordan", "Sam")
cups <- c(2, 4, 1)
likes_espresso <- c(TRUE, FALSE, TRUE)
coffee_habits <- data.frame(names, cups, likes_espresso)
coffee_habits
## names cups likes_espresso
## 1 Alex 2 TRUE
## 2 Jordan 4 FALSE
## 3 Sam 1 TRUE
dim(coffee_habits)
## [1] 3 3
coffee_habits$cups
## [1] 2 4 1
mean(coffee_habits$cups)
## [1] 2.333333
Task 8.1: Build your own small data frame with at least 3 columns and 3 rows, about any topic you like.
cookie_names <- c("choc chip", "snickerdoodle", "shortbread")
cookie_counts <- c(12, 24, 36)
good_with_espresso <- c(TRUE, TRUE, FALSE)
bakery_bites <- data.frame(cookie_names, cookie_counts, good_with_espresso)
bakery_bites
## cookie_names cookie_counts good_with_espresso
## 1 choc chip 12 TRUE
## 2 snickerdoodle 24 TRUE
## 3 shortbread 36 FALSE
Task 8.2: Check its dim(), then pull
out one numeric column with $ and find its
mean().
dim(bakery_bites)
## [1] 3 3
bakery_bites$cookie_counts
## [1] 12 24 36
mean(bakery_bites$cookie_counts)
## [1] 24
Question 8.2.1: Whatβs the actual difference between a data frame column and a standalone vector? Why is it useful to bundle several vectors together into one table?
A data frame column and a standalone vector are the same thing but when you combine them into table/data frame each vakue is aligned row by row, so that one
"observation" value stays aligned with its multiple variables and patterns can be identified
coffee_habits[1, ]
## names cups likes_espresso
## 1 Alex 2 TRUE
coffee_habits[, 2]
## [1] 2 4 1
coffee_habits[coffee_habits$cups > 1, ]
## names cups likes_espresso
## 1 Alex 2 TRUE
## 2 Jordan 4 FALSE
Task 8.3: Using your data frame from Task 8.1, pull
out just the first row, then just the first column, using
[row, column] indexing.
bakery_bites[1, ]
## cookie_names cookie_counts good_with_espresso
## 1 choc chip 12 TRUE
bakery_bites[, 1]
## [1] "choc chip" "snickerdoodle" "shortbread"
Task 8.4: Filter your data frame down to just the
rows where one of your numeric columns is above its own average, using
the df[condition, ] pattern.
bakery_bites[bakery_bites$cookie_counts > mean(bakery_bites$cookie_counts), ]
## cookie_names cookie_counts good_with_espresso
## 3 shortbread 36 FALSE
Question 8.4.1: What do you think
coffee_habits[2, ] returns, compared to
coffee_habits[, 2]? Try both and describe the
difference.
coffee_habits[2, ] returns the entire second row β
and all y values for that observation
coffee_habits[, 2] returns the entire second column β all x values for that variable
π Read Module 9 in the Workbook first.
sales_path <- file.path("..", "..", "data", "hoya_grounds_sales.csv")
sales <- read_csv(sales_path)
## Rows: 14 Columns: 6
## ββ Column specification ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
## Delimiter: ","
## chr (2): day_of_week, promo_ran
## dbl (3): day_number, cups_sold, temp_f
## lgl (1): is_weekend
##
## βΉ Use `spec()` to retrieve the full column specification for this data.
## βΉ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(sales)
## # A tibble: 6 Γ 6
## day_number day_of_week cups_sold temp_f is_weekend promo_ran
## <dbl> <chr> <dbl> <dbl> <lgl> <chr>
## 1 1 Mon 55 85 FALSE yes
## 2 2 Tue 60 62 FALSE yes
## 3 3 Wed 54 53 FALSE yes
## 4 4 Thu 41 82 FALSE no
## 5 5 Fri 57 46 FALSE yes
## 6 6 Sat 83 59 TRUE yes
Task 9.1: Run the chunk above. Confirm you see the
coffee cart sales data β that path is the same βup two, into
dataβ pattern from Module 7.
Task 9.2: Use nrow() and
ncol() to check how many rows and columns are in
sales.
nrow(sales)
## [1] 14
ncol(sales)
## [1] 6
Task 9.3: Pull out the cups_sold column
using sales$cups_sold, then find its
mean().
mean(sales$cups_sold)
## [1] 60
Task 9.4: Filter sales to just the rows
where cups_sold is greater than the columnβs average β same
df[condition, ] pattern you practiced on
coffee_habits in Module 8, just applied to a real
dataset.
sales[sales$cups_sold > mean(sales$cups_sold), ]
## # A tibble: 5 Γ 6
## day_number day_of_week cups_sold temp_f is_weekend promo_ran
## <dbl> <chr> <dbl> <dbl> <lgl> <chr>
## 1 6 Sat 83 59 TRUE yes
## 2 7 Sun 71 79 TRUE no
## 3 12 Fri 64 69 FALSE yes
## 4 13 Sat 64 67 TRUE no
## 5 14 Sun 91 74 TRUE yes
Question 9.4.1: Walk through, in order, everything
that happened between running read_csv(sales_path) and
being able to run mean(sales$cups_sold). What did the file
path do? What did read_csv() turn the file into? What did
$ do?
df[condition, ] allows us to ference a sataset or dataframe that is already existing versus establishing all of the vectors one by one and then linking them
Question F.1: Which module felt the most confusing? What specifically tripped you up?
7, understanding the pathways is sometimes visually confusing with all of the "." and "/"
Question F.2: Which module clicked the fastest for you?
mean/median/mode and the other math functions because I am comfortable with excel and its easy to visualize when I think of a graph
Question F.3: On a scale of 1-5, how ready do you feel for Session 1?
Kind of, 6