Week 11 · Chapter 11
Today’s steps, as a worksheet with blanks: Data Wrangling worksheet
11-wrangling-data.qmd in your Downloads folderHand-coding: reading each message yourself and recording its value by a written rule. Example: LULW alone is coded 1, a reaction; BIG TREE is coded 0, commentary.
Variable: something about a case that can change from one case to the next, where a case is one thing you study, such as one chat message.
Data wrangling: getting data into a form you can analyze, by tidying it (one row per case, one column per variable) and transforming it (keeping rows, adding columns, summarizing, joining tables). Example: turning the chat table’s timestamp digits into dates and adding a message length column.
Package: a bundle of functions, data, and help pages that adds to what R can do on its own. Example: the course package, v2v, adds the example Twitch data.
Tidyverse: a collection of R packages for data science that share one design and are built to work together. Example: library(tidyverse) loads its core packages at once, including dplyr for tables, lubridate for dates, and ggplot2 for charts.
filter() keeps the rows that meet a conditionselect() keeps or drops columnsmutate() adds a column or changes one, computed from other columnsarrange() reorders the rows by a column’s valuesgroup_by() with summarize() collapses many rows into one row per groupPipe (%>%): read it as “and then”; it hands the result of one line to the next. Example: chat %>% filter(channel == "bobross") takes the chat table, then keeps Bob Ross’s messages.
twitch_chat() and twitch_streams() load the two example tables from the v2v packageread_csv() from the tidyverse reads a CSV file into a table; that is how you load your own dataas_datetime() comes from lubridate, the tidyverse package for dates; library(tidyverse) loads itmutate() writes the result into a new ts column and leaves date as it wasdate counts milliseconds since 1970, and R’s date-times count seconds, so the line divides date by 1,000 before as_datetime() turns it into a date and timeymd_hms() or ymd() instead
mutate(ts = ymd_hms(posted_at)) reads text such as 2024-03-01 14:05:00mutate(day = ymd(posted_on)) reads text such as 2024-03-01ymd_hms() is the line you will most often needTimestamp: the date and time something happened. Example: 1542578127023 is the first message’s timestamp, 9:55 PM on November 18, 2018 (UTC).
date counts milliseconds since 1 January 1970
as_datetime() expects seconds, which are a thousand times coarserdate / 1000 does that division, so as_datetime() receives seconds# A tibble: 3 × 2
date ts
<dbl> <dttm>
1 1542578127023 2018-11-18 21:55:27
2 1542578135562 2018-11-18 21:55:35
3 1542578143610 2018-11-18 21:55:43
<dbl>, a decimal number, to <dttm>, a date-timestr_length() counts the characters in a string, R’s name for a piece of textmutate() writes the count into a new column# A tibble: 3 × 2
message message_length
<chr> <int>
1 "???????????????????????" 23
2 "TriEasy Clap TriEasy Clap TriEasy Cla…" 441
3 "cmonBruh" 8
message_length is a ratio variable: it has a true zero and equal intervals, so it can be averaged“Tidy data is a standard way of mapping the meaning of a dataset to its structure.”
Wickham (2014, p. 4)
Wickham (2014)
“Tidy data is a standard way of mapping the meaning of a dataset to its structure.”
Wickham (2014, p. 4)
Join: putting two tables together by matching a shared key, so each row picks up the other table’s columns. Example: every message picks up the game and viewers of its channel’s latest stream snapshot.
channel alonechannel and on time: the nearest snapshot at or before each messagegame and viewers of the latest earlier snapshot on its channelts from Transformation 1 no_snapshot n
1 FALSE 35256
2 TRUE 11
NA (missing)join_by(channel, closest(ts >= snapshot_ts)) says: same channel, and of the snapshots at or before the message, the closest onerename() gives the time column on one side its own name, because two columns both called ts would come back as ts.x and ts.y is_gaming n
1 FALSE 5
2 TRUE 43
3 NA 2
twitch_channels() loads the example lookup table; your own lookup table is a second file read with read_csv()small below 5,000 median viewers, medium up to 20,000, large above?twitch_channels to read both; your reviewer-facing comments 1 and 2 are about themmain_game is the category a channel streamed most often across its snapshots
is_gaming is FALSE for every one of his messagesmedian_viewers is the channel’s typical audience, and viewer_band cuts it into the three bandschannels is the lookup table: one row per channel, looked up by the joinleft_join() keeps every row and attaches each channel’s five label columns, matching on the shared channel keychat_streams already has a game column: the category streaming at that moment
main_game is the channel’s usual category, and later analyses use itKey: the column two tables share, which says which row in one goes with which row in the other. Example: channel, which reads bobross in both tables.
##bobross and another says bobrossv2v example data has matching keys. Raw data does not# A tibble: 3 × 2
is_gaming n
<lgl> <int>
1 FALSE 3457
2 TRUE 31309
3 NA 501
NA values hereNA Is the Correct OutcomeMissing value (NA): a cell with nothing in it, which R prints as NA. Example: the 501 messages from the two channels with no recorded category.
NANA records that the study cannot classify themviewer_band puts each channel in a band by its median viewers: small, medium, or largechannel_type writes is_gaming as words, “gaming” or “non-gaming”read_csv() lines and the same code runs on your own tablesdata/twitch_analysis.RDS, so save only from the worksheetanalysiswrite_rds() saves an RDS file, R’s own data file, with every column type intactRows: 35,267 Columns: 15
$ id, channel, sender, message, date: the five raw columns
$ ts <dttm> 2018-11-18 21:55:27, …
$ message_length <int> 23, 441, 8, …
$ snapshot_ts <dttm> NA, NA, NA, …
$ game <chr> NA, NA, NA, …
$ viewers <int> NA, NA, NA, …
$ main_game <chr> "Hand Simulator", "Just Chatting", …
$ is_gaming <lgl> TRUE, FALSE, TRUE, …
$ channel_type <fct> gaming, non-gaming, gaming, …
$ median_viewers <dbl> 22944, 16176, 10163, …
$ viewer_band <fct> large, medium, medium, …
NAmutate() and filter() is a methodological decisiondata/raw/ exactly as it arrived; never edit it or save over it
data/twitch_analysis.RDS, never back into it/ 1000could not find function "%>%" or "as_datetime": the tidyverse never loadedts.x and ts.y: the time column was not renamed before the joinNA: the keys disagree, often because of a #object 'channels' not found: the gray boxes of code (chunks) ran out of order; run them from the topcould not find function "twitch_channels": your v2v is older than 0.3.8: run AURA: Check my setup, then Install what is missingAURA: Explain an R error and press EnterYou should now have:
analysis table with 15 columns and 35,267 rowsdata/twitch_analysis.RDSts column of type <dttm> and a message_length column of type <int> (whole numbers)is_gaming column with 3,457 FALSE, 31,309 TRUE, and 501 NA