12 Setup: Vector Basics

Purpose: Vectors are the most important object we’ll work with when doing data science. To that end, let’s learn some basics about vectors.

Reading: Programming Basics. Topics: vectors Reading Time: ~10 minutes

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.4.0      ✔ purrr   1.0.1 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.5.0 
## ✔ readr   2.1.3      ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

12.0.1 q1 What single-letter R function do you use to create vectors with specific entries? Use that function to create a vector with the numbers 1, 2, 3 below.

vec_q1 <- c(1, 2, 3)

vec_q1
## [1] 1 2 3

Use the following tests to check your work:

## NOTE: No need to change this
assertthat::assert_that(length(vec_q1) == 3)
## [1] TRUE
assertthat::assert_that(mean(vec_q1) == 2)
## [1] TRUE
print("Nice!")
## [1] "Nice!"

12.0.2 q2 Did you know that you can use c() to extend a vector as well? Use this to add the extra entry 4 to vec_q1.

vec_q2 <- c(vec_q1, 4)

vec_q2
## [1] 1 2 3 4

Use the following tests to check your work:

## NOTE: No need to change this
assertthat::assert_that(length(vec_q2) == 4)
## [1] TRUE
assertthat::assert_that(mean(vec_q2) == 2.5)
## [1] TRUE
print("Well done!")
## [1] "Well done!"