S23 · Chapter 12
| What | Due | Have done by |
|---|---|---|
| Data Wrangling in R, 50 points | Fri, Nov 13, 11:59 PM | Codes saved as a CSV before Thursday’s lab |
| Describing Data in R, 75 points | Fri, Nov 20, 11:59 PM | Started in Thursday’s lab |
| Weekly reading journal | Fri, Nov 13 and Fri, Nov 20, 11:59 PM | Chapter 12, then Chapter 13 |
No class Nov 23 to 29: Thanksgiving Break.
| What | Due | Have done by |
|---|---|---|
| Inferencing Data in R, 100 points | Fri, Dec 4, 11:59 PM | Taught Nov 17 and 19 |
| Coded dataset pushed (White Paper dated step) | Fri, Dec 4, 11:59 PM | First coding pass done by Tue, Dec 1 |
| Week 15 reading journal | Fri, Dec 4, 11:59 PM | Chapter 14 |
| White Paper, 250 points | Fri, Dec 18, 11:59 PM | Results built from your own data |
Data Wrangling worksheet, Task 0.3
CSV: a table saved as plain text, one row per line, with a comma between each column.
data/coding-form.csv, so the file must be a CSVdata folder and name it coding-form.csvid column exactly as it iscoding-form - Sheet1.csvcoding-form.csv inside data, click Delete, and confirmdata, click Rename…, type coding-form.csv, and press Enterread_csv() reads a CSV file into a tableglimpse() prints how many rows and columns it has, and each column’s type"data/coding-form.csv" means: start in the folder open in VS Code, go into data, open coding-form.csv
> read_csv("data/coding-form.csv") %>%
+ glimpse()
Rows: 30 Columns: 5
── Column specification ────────────────────────────────────────────────
Delimiter: ","
chr (2): channel, message
dbl (2): id, reaction_only
lgl (1): coder_notes
ℹ 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.
Rows: 30
Columns: 5
$ id <dbl> 16240927, 182383, 19530370, 12102429, 7123505, 2…
$ channel <chr> "bobross", "bobross", "bobross", "bobross", "bob…
$ message <chr> "press F to bring back bob", "BIG TREE", "CoolSt…
$ reaction_only <dbl> 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, …
$ coder_notes <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
Rows: 30, Columns: 5, and reaction_only as <dbl> (a number): the sheet is right> read_csv("data/coding-form.csv") %>%
+ glimpse()
Error: 'data/coding-form.csv' does not exist in current working directory ('C:/Users/you/Documents/mc451-workspace').
does not exist: no file in data has exactly that namelist.files("data") lists every file in the data folder> list.files("data")
[1] "coding-form - Sheet1.csv" "twitch_analysis.RDS"
coding-form.csv in the VS Code Explorer, then run the check againDescribing Data, 75 points, due Friday, November 20, 11:59 PM
analysis table of about 35,000 rows
Your first version of each figure will look wrong. Fixing it takes about three lines.
data/twitch_analysis.RDS, with your thirty codes insideMC 451 worksheet · Describing Data risks guide
Three lines start every Describing session: two packages, then the table Data Wrangling saved.
library() switches a package on for this sessionread_rds() reads the saved file and names the table analysiscannot open the connection, the file is not in your data folder> library(tidyverse)
── Attaching core tidyverse packages ──────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.6
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.1 ✔ tibble 3.3.0
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.2.0
── Conflicts ────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
> library(v2v)
v2v 0.3.9 loaded.
setup() checks that R, Quarto and Git are ready
?v2v lists what this package gives you
> analysis <- read_rds("data/twitch_analysis.RDS")
masks lines are normal, not errorsread_rds() prints nothing: the table went into analysis quietlyglimpse() prints one line per column: its name, its type, and its first few values> glimpse(analysis)
Rows: 35,267
Columns: 16
$ id <dbl> 89, 551, 1033, 2094, 2803, 10406, 16104, 22571,…
$ channel <chr> "sodapoppin", "xqcow", "forsen", "sodapoppin", …
$ sender <chr> "madzee", "prometheanow", "spectre155", "itsjer…
$ message <chr> "???????????????????????", "TriEasy Clap TriEas…
$ date <dbl> 1.542578e+12, 1.542578e+12, 1.542578e+12, 1.542…
$ ts <dttm> 2018-11-18 21:55:27, 2018-11-18 21:55:35, 2018…
$ message_length <int> 23, 441, 8, 12, 206, 37, 6, 7, 4, 7, 3, 4, 12, …
$ snapshot_ts <dttm> NA, NA, NA, NA, NA, 2018-11-18 21:56:40, 2018-…
$ game <chr> NA, NA, NA, NA, NA, "Artifact", "Marble It Up!"…
$ viewers <int> NA, NA, NA, NA, NA, 15300, 28076, 4703, 19239, …
$ main_game <chr> "Hand Simulator", "Just Chatting", "The Elder S…
$ is_gaming <lgl> TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRU…
$ channel_type <fct> gaming, non-gaming, gaming, gaming, non-gaming,…
$ median_viewers <dbl> 22944, 16176, 10163, 22944, 16176, 10163, 22944…
$ viewer_band <fct> large, medium, medium, large, medium, medium, l…
$ reaction_only <fct> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
reaction_only is NA except on your thirty coded messagesggplot2 is not a catalog of chart types. It is one idea from Wilkinson’s The Grammar of Graphics: every plot is built from the same few parts.
Choose those three parts and the plot is fully specified.
ggplot() names the data and, inside aes(), says which column maps to which visual property. geom_line() says what mark to draw, and + stacks layers on top of each other.
Swap geom_line() for geom_col() and the same data is drawn as bars.
Pick one variable from your own codebook.
> viewers_over_time <- twitch_viewers_over_time()
> viewers_over_time
# A tibble: 97 × 3
six_hour category total_viewers
<dttm> <fct> <int>
1 2018-11-18 18:00:00 Art 238217
2 2018-11-18 18:00:00 Just Chatting 2034123
3 2018-11-18 18:00:00 Pokemon: Let's Go, Pikachu!/Eevee! 99909
4 2018-11-18 18:00:00 Other 4333423
5 2018-11-19 00:00:00 Art 870710
6 2018-11-19 00:00:00 Fortnite 258780
7 2018-11-19 00:00:00 Just Chatting 4312125
8 2018-11-19 00:00:00 Pokemon: Let's Go, Pikachu!/Eevee! 1991196
9 2018-11-19 00:00:00 Other 6933016
10 2018-11-19 06:00:00 Art 12444
# ℹ 87 more rows
count() makes one row per category and counts the rows in each?twitch_viewers_over_time says how> viewers_over_time %>%
+ count(category)
# A tibble: 6 × 2
category n
<fct> <int>
1 Art 21
2 Fortnite 17
3 Hearthstone 9
4 Just Chatting 18
5 Pokemon: Let's Go, Pikachu!/Eevee! 10
6 Other 22
n is how many six-hour blocks each one hasggplot(viewers_over_time,
aes(x = six_hour, y = total_viewers,
color = category)) +
geom_line(linewidth = 0.9) +
scale_y_continuous(labels = scales::label_comma()) +
labs(title = "Concurrent viewers by game category",
x = "Date (UTC, six-hour buckets)",
y = "Total viewers in bucket",
color = "Category") +
v2v::scale_colour_v2v() +
v2v::theme_v2v()Time goes on the x axis, summed viewers on the y axis, and category to color. label_comma() writes 20,000,000 instead of 2.0e+07, and theme_v2v() gives every figure in the set one consistent look.
A dominant series crowds out the rest. Notice that in your own figure, and say so in the caption.
Hour of day is 24 separate categories, and we want a count of messages in each one.
hour() pulls the hour, 0 to 23, out of each message’s time stampcount() then counts the messages in each hour and names the count messages> chat_by_hour <- analysis %>%
+ mutate(hour = hour(ts)) %>%
+ count(hour, name = "messages")
> chat_by_hour
# A tibble: 24 × 2
hour messages
<int> <int>
1 0 1816
2 1 1959
3 2 1013
4 3 833
5 4 1384
6 5 1317
7 6 1393
8 7 1165
9 8 1016
10 9 1381
# ℹ 14 more rows
arrange() sorts the rows; desc() puts the biggest count first> chat_by_hour %>%
+ arrange(desc(messages))
# A tibble: 24 × 2
hour messages
<int> <int>
1 13 2201
2 16 2132
3 17 2084
4 1 1959
5 12 1915
6 0 1816
7 11 1775
8 18 1717
9 15 1578
10 10 1449
# ℹ 14 more rows
geom_col() draws a bar whose height is a number you already computed.
Twitch, November 2018
Twenty-four numbers become a rhythm you can read in one glance.
Histogram: a bar chart of a number variable; each bar counts the values in one range, such as the messages 0 to 5 characters long.
First, the table the histogram draws from:
filter() drops the messages from channels with no gaming labelpmin() keeps each length, but shows anything over 120 as 120count(is_gaming) checks how many messages each group has left> msglen <- analysis %>%
+ filter(!is.na(is_gaming)) %>%
+ mutate(length_shown = pmin(message_length, 120))
> msglen %>%
+ count(is_gaming)
# A tibble: 2 × 2
is_gaming n
<lgl> <int>
1 FALSE 3457
2 TRUE 31309
FALSE is non-gaming: 3,457 messages, about one in tenTRUE is gaming: 31,309 messagesmax() finds the biggest value; na.rm = TRUE skips the empty messages> msglen %>%
+ summarize(
+ longest = max(message_length, na.rm = TRUE),
+ longest_shown = max(length_shown, na.rm = TRUE)
+ )
# A tibble: 1 × 2
longest longest_shown
<int> <dbl>
1 501 120
binwidth = 5 makes each bar five characters wide; alpha lets the overlap show throughThe terminal also prints this under the histogram code:
> ggplot(msglen,
+ aes(x = length_shown, fill = is_gaming)) +
+ geom_histogram(binwidth = 5, position = "identity",
+ alpha = 0.55) +
+ labs(x = "Message length (characters, capped at 120)",
+ y = "Messages",
+ fill = "Gaming channel") +
+ v2v::scale_fill_v2v() +
+ v2v::theme_v2v()
Warning message:
Removed 4 rows containing non-finite outside the scale range
(`stat_bin()`).
binwidth = 5 makes each bar five characters wide
pmin(message_length, 120) caps the display at 120 characters
An unannounced cap is a quiet distortion. Label the axis with the cap, as labs(x = ...) did.
Skew: a lopsided shape. Right-skewed means most values are small and a few are very large, like message length here.
The skew is the most important thing the figure reveals, and no single summary number can show it.
Mean: the average, the total divided by the count. Median: the middle value once the values are sorted. Example: the mean message is 29.0 characters long, the median 17.
group_by(is_gaming) splits the table into the two groupssummarize() makes one row per group: the count (n), the mean, the median, and the standard deviation (sd)na.rm = TRUE leaves the four empty messages out of each number> msglen %>%
+ group_by(is_gaming) %>%
+ summarize(
+ n = sum(!is.na(message_length)),
+ mean = mean(message_length, na.rm = TRUE),
+ median = median(message_length, na.rm = TRUE),
+ sd = sd(message_length, na.rm = TRUE),
+ .groups = "drop"
+ )
# A tibble: 2 × 5
is_gaming n mean median sd
<lgl> <int> <dbl> <int> <dbl>
1 FALSE 3457 33.7 16 60.7
2 TRUE 31305 28.5 17 38.5
Standard deviation (sd): how far values typically sit from the mean. Example: non-gaming chat’s sd of 60.7 says its lengths are more spread out than gaming chat’s 38.5.
The gap in means is real arithmetic, but it is the work of the tail, not of the typical message.
You should now be able to:
theme_v2v()If a figure will not render, check that every + ends the line above it.
data/twitch_analysis.RDS is in your workspace’s data folderA figure can frame the question. It cannot answer it.