Coding the Codebook

S17 · Chapter 9 · MC 451 Research Methods in Mass Media

Dr. Alex Leith

Today’s Agenda

  • A project folder that holds the whole study
  • Your codebook installed as a real document
  • The data loaded, and looked at for the first time
  • What to do when a line breaks

What We Build Today

Lab session

  • A project folder holding the whole study
    • Your Chapter 8 codebook, installed as a document
    • The Twitch data, loaded and looked at
  • No analysis today
    • We open, we load, we look
  • Everything here becomes your White Paper method

Why the Folder Comes First

  • You designed a study and never saw the data
  • Reproducible work lives in files in known places
    • Not in commands typed once
  • A project is one folder
    • Data, code, codebook, and report together
    • Nothing depends on a file only you have

Deciding where files go is friction, so we skip that decision.

Scaffolding the Project

v2v::new_portfolio("twitch-portfolio")

That one line creates the whole folder, already named and already wired together.

Note the v2v:: prefix. It says the function comes from this book’s package, so the package never becomes a hidden assumption.

What the Scaffold Gives You

twitch-portfolio/
├── twitch-portfolio.Rproj
├── _quarto.yml
├── README.md
├── codebook.qmd
├── analysis.qmd
├── data/
└── .Rprofile

You did not decide where anything goes or what to name it, which is the point.

Confirming the Stack

v2v::setup()

setup() checks that R, the packages this book depends on, and the project environment are all present and agree on versions.

  • You met it once, when you installed the stack
  • Run it every time you open the project
  • You learn about a missing piece before it interrupts you

The Rulebook Becomes a Document

  • One file in the scaffold is codebook.qmd
    • In Chapter 8 your codebook was an argument on paper
    • Now it is the reference every later step answers to
  • Move the draft in, add a version line and a date
  • A codebook that renders cleanly is one a coder can read

Your Turn

We are about to run glimpse(chat), which prints every column.

  • What columns do you predict are in there? Name three
  • Your codebook needs message text and sender. What if one is missing?
  • What would you do if a column your study depends on is absent?

Loading the Data

library(tidyverse)

chat <- v2v::twitch_chat()
streams <- v2v::twitch_streams()

Line one loads the tidyverse; the next two fetch each table and name it with <-.

The data arrives through a function call, not a file path, which removes a whole category of beginner error. You get the working sample, not the full 2018 collection.

Looking, the Chat Table

glimpse(chat)
Rows: 35,267
Columns: 5
$ id      <int> 89, 551, 1033, 2094, 2803, …
$ channel <chr> "sodapoppin", "xqcow", "forsen", …
$ sender  <chr> "madzee", "prometheanow", "spectre155", …
$ message <chr> "???????????????????????", "TriEasy Clap TriEasy…
$ date    <dbl> 1542578127023, 1542578135562, 1542578143610, …

glimpse() prints every column, its type, and its first few values.

Reading That Output

  • 35,267 rows, five columns, one message per row
  • The first two messages are not errors
    • Twenty-three question marks, then copypasta
    • That is what Twitch chat contains
  • date is a <dbl> and completely unreadable
    • Milliseconds since 1970, fixed in Chapter 11

Looking, the Stream Table

glimpse(streams)
Rows: 32,276
Columns: 6
$ channel <chr> "sodapoppin", "xqcow", "forsen", "giantwaffle", …
$ title   <chr> "Hello truckers\n44835158804929_2115841973", …
$ game    <chr> "Marble It Up!", "Just Chatting", "Artifact", …
$ viewers <int> 27934, 19381, 15300, 4697, 28076, …
$ date    <dbl> 1542578200214, 1542578200240, 1542578200273, …

Each row is one snapshot of one stream at one moment.

Counting What Channels Stream

streams %>% count(game, sort = TRUE)
   game                     n
 1 Art                   5298
 2 Fortnite              3682
 3 Just Chatting         2474
 4 Hearthstone           2157
 6 League of Legends     1952
# ℹ 50 more rows

count() tallies how often each value appears, and sort = TRUE puts the biggest first.

Confirming the Corpus

n_distinct(chat$channel)
[1] 50
  • Fifty channels, the corpus described since Chapter 1
  • n_distinct(streams$channel) returns the same fifty
  • Sixty game categories across fifty channels
    • So most channels move around

Counting is how you check the data is what you were told it is.

When a Line Breaks

  • R will return an error instead of a result
    • A misspelling, a package not loaded, a stray bracket
    • This happens to everyone, and is no verdict on you
  • The Hub keeps a common-errors cheat sheet
  • An error message is information, so read it first

Run ?v2v::common_errors for the ones specific to this data.

Checkpoint

You should now have:

  • A twitch-portfolio folder, from new_portfolio()
  • A clean v2v::setup() result
  • Your codebook in codebook.qmd, versioned and rendered
  • chat and streams loaded, and a glimpse() of each

Nothing changed in the data. Looking is all we did.

Before Thursday

  • Finish moving your codebook into codebook.qmd and render it
  • Due this week: Codebook and Qual Memo
    • The full codebook plus a 200 to 300 word memo
  • Bring your glimpse() output on Thursday
    • We read the data against your codebook

Questions?