wk5-d01-data-types
Chico State
DATA 385 - Fall 2026
September 22, 2026
🎥 Data types - code along in wk5-d01
New Packages
here: The here package creates paths relative to the top-level directory. We will talk more about this in wk5-d03.
A survey asked respondents their name and number of cats. The instructions said to enter the number of cats as a numerical value.
Important
Do not add the notes part of the file path above to your code file.
# A tibble: 60 × 3
name number_of_cats handedness
<chr> <chr> <chr>
1 Bernice Warren 0 left
2 Woodrow Stone 0 left
3 Willie Bass 1 left
4 Tyrone Estrada 3 left
5 Alex Daniels 3 left
6 Jane Bates 2 left
7 Latoya Simpson 1 left
8 Darin Woods 1 left
9 Agnes Cobb 0 left
10 Tabitha Grant 0 left
# ℹ 50 more rows
What is the type of the number_of_cats variable?
# A tibble: 1 × 1
mean_cats
<dbl>
1 0.833
Don’t mix data types mid-stream. Recode as a character, and then convert to numeric afterward.
Reassign the result of these steps back to cat_lovers.
Dr. D’s note - I don’t like to overwrite my data set too often. I’m bound to make mistakes so I like to create a new variable every once in a while when it makes sense. Like moving from individual records to grouped/aggregated records.
Vectors can be constructed using the c() function.
R will happily convert between various types without complaint when different types of data are concatenated in a vector, and that’s not always a great thing!
as.logical(), as.numeric(), as.integer(), as.double(), or as.character()Ignore this. We’re not doing that Hotels + Data types
Example
Suppose we want to know the type of c(1, "a"). First, look at the type of each piece on its own, and guess based on those. Then check the combined vector directly.
NAs are special snowflakes[1] NA
[1] 2.5
Min. 1st Qu. Median Mean 3rd Qu. Max. NAs
1.00 1.75 2.50 2.50 3.25 4.00 1
Note! mean() requires na.rm = TRUE to drop the NA, but summary() handles it automatically and just reports the count.
NAs are logicalR uses NA to represent missing values in its data structures.
NAsNaN, NAs are genuinely unknown valuesWhy do the following give different answers?
NAs are logical, explainedNA is unknown, so it could be TRUE or FALSE:
Note
This page adapts material from Data Science in a Box (Unit 2, Deck 10: “Data types”) by Mine Çetinkaya-Rundel, licensed under CC BY-SA 4.0. Source: tidyverse/datascience-box. Modified: converted from xaringan to Quarto revealjs.