S22 · Chapter 11
ts exists, and message_length existsThe course package did the first two steps for all fifty channels. The join is yours.
select() keeps four columns and renames ts to snapshot_ts on the wayts columnleft_join() keeps every message and attaches the game and viewers of the latest snapshot taken at or before it, on the same channeljoin_by(channel, closest(ts >= snapshot_ts)) is the key: same channel, nearest earlier snapshot no_snapshot n
1 FALSE 35256
2 TRUE 11
NA (missing)Rolling join: a join whose key matches the nearest value instead of an exact one. Example: each message takes the latest snapshot at or before its own time, because no snapshot shares a message’s exact second.
closest(ts >= snapshot_ts) is the rolling part: the snapshot time closest to the message time, without passing it is_gaming n
1 FALSE 5
2 TRUE 43
3 NA 2
twitch_channels() loads the example lookup table, built once for the courseTwo channels have no game recorded in any snapshot. Use Chapter 11, Labeling gaming and non-gaming.
channels hold for such a channel in main_game? In is_gaming?is_gaming? Answer in one wordsmall below 5,000 median viewers, medium up to 20,000, large above?twitch_channels to read both, so a reviewer can check 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 bandschat_streams <- at its start to analysis <-left_join() keeps every row of chat_streams, attaches each channel’s five label columns, and names the result analysisJoin: putting two tables together by matching a shared key, so each row picks up the other table’s columns. Example: every message picks up its channel’s main_game and is_gaming.
by = "channel" names the key column both tables sharechannels is the lookup table: one row per channel, looked up by the joinKey: 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 has matching keys; raw Twitch data does not is_gaming n
1 FALSE 3457
2 TRUE 31309
3 NA 501
count() on a new column is the quickest check there isNA, alongside TRUE and FALSEMissing 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.
game, so there was no most frequent categorychannels holds NA for them, so the join left their messages as NAchat <- twitch_chat() %>%
mutate(ts = as_datetime(date / 1000),
message_length = str_length(message))
snapshots <- twitch_streams() %>%
mutate(ts = as_datetime(date / 1000)) %>%
select(channel, snapshot_ts = ts, game, viewers)
channels <- twitch_channels()
analysis <- chat %>%
left_join(
snapshots,
by = join_by(channel, closest(ts >= snapshot_ts))
) %>%
left_join(channels, by = "channel")write_rds() saves the table as an RDS file, R’s own data file, with every column type intact
Rows: 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, …
snapshot_ts and viewers open with NAFactor: R’s way of storing a category, with its labels in an order you choose. Example: viewer_band stored as small, medium, large.
read_csv(), then clean_names()factor() turns 0 and 1 into labeled groups, commentary and reactionleft_join(codes, by = "id") attaches them: the coding sheet’s id is the chat idNA on reaction_only, and that is correctNA: the keys do not match, so check for a leading “#”object 'snapshot_ts' not found: the select() that renames the snapshot time never ran, so make snapshots firstobject 'channels' not found: R has no object (no named result) called that yet: you ran the join before loading the lookup tablecould not find function "twitch_channels": your v2v is older than 0.3.8: run AURA: Check my setup, then Install what is missingYou should now have:
channels lookup table, one row per channel, with main_game, is_gaming, channel_type, median_viewers, and viewer_bandanalysis table of 16 columns: the 15 shown earlier plus reaction_onlycount(is_gaming) result you have actually looked atdata/twitch_analysis.RDS saved