15.3 R and RStudio refresher

15.3.1 Project organization

Use a project folder with separate locations for raw data, processed data, code, functions, figures, tables, and documentation. Raw data should remain unchanged. Paths should be project-relative rather than tied to one person’s computer.

15.3.2 Import and inspection

Import dates, missing values, and character fields deliberately. Confirm dimensions, names, types, ranges, and keys immediately after import. Spreadsheet display can hide leading zeros, date conversions, and numbers stored as text.

15.3.3 Transformation

Write transformations as readable steps. Preserve source fields and create standardized or derived variables with documented rules.

library(dplyr)

member_summary <- visits |>
  filter(visit_date >= analysis_start,
         visit_date <= analysis_end) |>
  group_by(member_id) |>
  summarise(
    visits = n_distinct(visit_episode_id),
    centres_used = n_distinct(centre_id),
    last_visit = max(visit_date),
    .groups = "drop"
  )

The code states the period, episode definition, grouping unit, and summaries. Those choices should agree with the dictionary.

15.3.4 Grouped summaries

retention_by_channel <- members |>
  filter(retention_eligible) |>
  group_by(acquisition_channel) |>
  summarise(
    eligible = n(),
    retained = sum(retained_12m),
    retention_rate = mean(retained_12m),
    .groups = "drop"
  )

The eligibility filter is essential. Without it, recent members could be misclassified or excluded inconsistently.

15.3.5 Reproducible outputs

Tables and figures should be generated from code whenever feasible. Set seeds for stochastic procedures, record package versions, and avoid manual edits to exported results. The R references provide deeper programming and EDA review (Peng 2020b, 2020a).

References

Peng, Roger D. 2020a. Exploratory Data Analysis with r. Leanpub.
———. 2020b. R Programming for Data Science. Leanpub.