S24 · Chapter 12
| What | Due | Have done by |
|---|---|---|
| Data Wrangling in R, 50 points | Fri, Nov 13, 11:59 PM | Codes saved as a CSV today |
| Describing Data in R, 75 points | Fri, Nov 20, 11:59 PM | Started today, after Data Wrangling renders |
| 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 |
35267, Data Wrangling is nearly done
Say out loud when you are stuck. That is what this session is for.
CSV: a table saved as plain text, one row per line, with a comma between each column.
data folder and name it coding-form.csvlist.files() lists what is in the folder R started in> list.files()
[1] "_templates" "00-check-your-setup.qmd"
[3] "00_Inbox" "01_Journal"
[5] "02_Literature" "03_Project"
[7] "04_Resources" "11-wrangling-data.qmd"
[9] "data" "LICENSE"
[11] "README.md"
data is in the list, beside README.mddata folderdata and press Enter (Return on a Mac)data sits beside README.md, not inside another folder"data" again means: start in the folder open in VS Code, go into data> list.files("data")
[1] "coding-form.csv" "twitch_analysis.RDS"
coding-form.csv: your thirty codes, saved as a CSVtwitch_analysis.RDS: the table Part 7 savedread_rds() loads the saved file back, and nrow() counts its rows> read_rds("data/twitch_analysis.RDS") %>%
+ nrow()
[1] 35267
35267: one row per chat message, nothing lost and nothing addedQuarto: Render Document and press EnterThe last line of the Terminal panel:
Output created: 11-wrangling-data.html
Output created means it worked11-wrangling-data.html appears beside the worksheet in the Explorer.qmd file is saved inREADME.md: both starts are the same folder03_Project: render looks for data inside that folder, and stopsThe same worksheet, saved in 03_Project and rendered:
Error:
! 'data/coding-form.csv' does not exist in current working directory ('C:/Users/you/Documents/mc451-workspace/03_Project').
Backtrace:
▆
1. ├─read_csv("data/coding-form.csv") %>% clean_names()
2. ├─janitor::clean_names(.)
3. ├─readr::read_csv("data/coding-form.csv")
4. │ └─vroom::vroom(...)
5. │ └─vroom:::vroom_(...)
6. └─vroom (local) `<fn>`("data/coding-form.csv")
7. └─vroom:::check_path(path)
Quitting from 11-wrangling-data.qmd:425-429 [read-codes]
Execution halted
03_Project: that is where render startedREADME.md, and render againData Wrangling in R, Blackboard
11-wrangling-data.qmd, with no ___ left11-wrangling-data.html, the rendered pagedata/coding-form.csv, your thirty codesdata/twitch_analysis.RDS, the saved tableREADME.md, with an entry or the no-AI box checkedDescribing Data, 75 points, due Friday, November 20, 11:59 PM
12-describing-data-mc451.qmd, saved in your workspace’s top folder beside README.md
glimpse() prints Rows: 35,267 and Columns: 16filter() 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 messagesTRUE is gaming: 31,309 messages> 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
| Channels | Messages | Mean | Median |
|---|---|---|---|
| Non-gaming | 3,457 | 33.7 | 16 |
| Gaming | 31,305 | 28.5 | 17 |
Example data: episodes_analysis.RDS, Power Rangers episodes coded for threat and focus
Cross-tabulation: a grid that counts every combination of two categories.
count() with two columns: one row per combinationpivot_wider() turns those rows into a grid, one column per focus code> episodes <- read_rds("data/episodes_analysis.RDS")
>
> episodes %>%
+ filter(!is.na(focus)) %>%
+ count(threat, focus) %>%
+ pivot_wider(names_from = focus,
+ values_from = n,
+ values_fill = 0)
# A tibble: 3 × 4
threat team one_ranger other
<fct> <int> <int> <int>
1 monster 15 21 1
2 villain 29 18 9
3 none 10 13 3
threat code, one column per focus codemonster and teamgroup_by(threat) makes sum(n) add up within each threat code> episodes %>%
+ filter(!is.na(focus)) %>%
+ count(threat, focus) %>%
+ group_by(threat) %>%
+ mutate(share = round(n / sum(n), 3))
# A tibble: 9 × 4
# Groups: threat [3]
threat focus n share
<fct> <fct> <int> <dbl>
1 monster team 15 0.405
2 monster one_ranger 21 0.568
3 monster other 1 0.027
4 villain team 29 0.518
5 villain one_ranger 18 0.321
6 villain other 9 0.161
7 none team 10 0.385
8 none one_ranger 13 0.5
9 none other 3 0.115
one_ranger: 0.568 of the monster episodes, 0.321 of the villain onesRemoved 4 rows: the four empty messages, which have no lengthpmin(), say so in the axis labelfct_reorder() sorts the bars by size; coord_flip() turns them sideways so long names fitposition = "identity" with alpha overlays the two groups so their shapes can be comparedlabs() sets them: x, y, and fill in Figure Threescale_colour_v2v() and scale_fill_v2v(), which are built to be safeAlt text: a sentence that says what a chart shows, for a reader who cannot see it. A screen reader reads it aloud.
fig-alt attributealt = line marked YOUR TURN inside labs(), and a fig-alt line when it opens a new chunk: write the sentence in bothggsave() writes the last chart drawn to a file, at a size you set"figures/..." starts in the folder open in VS Code, like data> dir.create("figures", showWarnings = FALSE)
> ggsave(
+ "figures/fig-top-categories.png",
+ width = 7, height = 4.5, dpi = 300
+ )
> list.files("figures")
[1] "fig-top-categories.png"
ggsave() prints nothing when the size is setlist.files("figures") shows the saved fileobject 'analysis' not found: R has no object (no named result) called analysis yet: you never ran the load linecannot open the connection, with No such file or directory under it: twitch_analysis.RDS is not in the data folder beside README.mdcould not find function "ggplot": the tidyverse has not loadedaes() names a column that does not existError in +: a + is missing, or one is trailing at the endYou should now have, in your Describing Data worksheet:
theme_v2v() and fully labeled