13 Setup: Types
Purpose: Vectors can hold data of only one type. While this isn’t a course on computer science, there are some type “gotchas” to look out for when doing data science. This exercise will help us get ahead of those issues.
Reading: Types
## ── 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()
13.1 Objects vs Strings
13.1.1 q1 Describe what is wrong with the code below.
## TASK: Describe what went wrong here
## Set our airport
airport <- "BOS"
## Check our airport value
airport == ATL
Observations:
ATL
(without quotes) is trying to refer to an object (variable); we would need to write"ATL"
(with quotes) to produce a string.
13.2 Casting
Sometimes our data will not be in the form we want; in this case we may need to cast the data to another format.
as.integer(x)
converts to integeras.numeric(x)
converts to real (floating point)as.character(x)
converts to character (string)as.logical(x)
converts to logical (boolean)