Day 1 · 09:30 · 75 min

Computational thinking

How a machine actually holds a number

Day 1 · 09:30

A computer does not ‘understand’ your P&L. It stores types, applies functions, and writes files. Once you can see that, R stops being mysterious and starts being a colleague who never gets bored of a 200,000-row extract.

You will leave able to

  • Assign variables, distinguish types, and call a function with named arguments.
  • Load a CSV of the LBS cohort survey and rename its columns.
  • Produce a first chart you would not be ashamed to put on a warm-up slide.

On the desk

Coding is a skill that can be picked up by people from all backgrounds, for any kind of data.

Course principle — Lagos Business School Executive Education

This is the most basic introduction to R that a commercial audience needs. We will not start with statistics. We will start with how a machine holds a fact, because every later disaster — a chart that double-counts SKUs, a join that inflates revenue — is a type error wearing a suit.

The four panes

Posit Desktop shows four panes: the script (what you want to keep), the console (where you try things), the environment (what currently exists in memory), and the viewer (plots, help, files). Treat the console as a whiteboard. Treat the script as the minute of the meeting.

In a Quarto / R Markdown file, code lives in chunks fenced with three backticks and {r}. Run one chunk, or run all. Lines starting with # are comments — they are for the next human, including you in six weeks.

Variables and assignment

R
x <- 1 + 5
x
# 6
# Spoken: "x gets 1 + 5"
# The environment pane now lists x.

<- is assignment. = also works in most places; this faculty prefers <- in scripts because it cannot be confused with a function argument. Name things after the business object (nps_branch, otif_lagos), not after the worksheet (Sheet3_final_v7).

Types you will actually meet

R
typeof(x) # "double" — a number
first_string <- "Lagos"
first_vector <- c(2, 4, 6)
first_integer <- 4L
first_logical <- TRUE
second_value <- first_vector[2] # 4 — R is 1-indexed
  • double / numeric — revenue, NPS, FX. Almost every ‘number’ in a CSV is a double.
  • character — SKU names, branch codes, comments. Never do arithmetic on these.
  • logicalTRUE / FALSE. Filters are made of these.
  • factor — a character with a known set of levels (channel, region). Useful, easy to misuse.
  • vector — an ordered collection of one type. A column of a table *is* a vector.

Functions

A function is a named recipe. You call it with arguments in parentheses. Packages (libraries) are just boxes of functions someone else already tested.

R
library(tidyverse)
# anatomy
# function_name(argument1, argument2, ...)
mean(first_vector)
round(mean(c(58, 42, 47)), 1)

Load the cohort survey

Download lbs-exec-survey.csv· 48 anonymised rows, this week’s room
R
survey <- read_csv("lbs-exec-survey.csv")
glimpse(survey)
# If names arrived ugly from a form export:
survey <- survey %>%
janitor::clean_names()

Click the object in the Environment pane. You should see function, sector, years_in_role, coding_experience, data_confidence, primary_tool, and goal. This is the same shape as an employee engagement extract or a branch census.

Function mix in this week's LBS executive cohort (n = 48). Finance and operations still dominate the room.
R
survey %>%
count(function, sort = TRUE) %>%
ggplot(aes(n, fct_reorder(function, n))) +
geom_col(fill = "#0B1F3A") +
labs(
title = "Who is in the room",
x = "Delegates",
y = NULL
) +
theme_minimal()

Exercise 1.1

Confidence by primary tool

Using survey, produce a table of mean data_confidence by primary_tool. Who claims to be most confident — and is that the group you would trust with a board pack?

  • group_by(primary_tool) %>% summarise(mean_conf = mean(data_confidence), n = n())
  • Excel users often rate themselves higher than SQL users. Confidence is not competence.