Foundations of R & RStudio

DMgtAsia · Session-I

Data science for public health

  • Public health runs on data: surveys, surveillance, registers, trials.
  • Data science is the workflow that turns that data into decisions: import, tidy, transform, visualise, model, communicate.
  • We do it in a way others can check and repeat.

Why R and RStudio

  • R is a language built by and for statisticians and epidemiologists.
  • It is free, open, and has a package for almost every method in public health.
  • RStudio is the workbench that makes R comfortable to use.
  • Everything is scripted, so an analysis is a document you can re-run, not a set of clicks.

The RStudio tour

  • Source (top left): your scripts and Quarto documents.
  • Console (bottom left): R runs here.
  • Environment (top right): the objects you have created.
  • Files / Plots / Help (bottom right): outputs and documentation.
  • Projects keep one analysis self-contained; with here(), file paths just work.

Objects and assignment (live)

Press Run. <- stores a value in a named object.

The keyboard shortcut for <- is Alt/Option and minus.

Quiz: assignment

Vectors (live)

A vector holds several values of the same type. c() combines them.

Try changing a number and running again.

Data types

  • numeric 3.4, counts and measurements.
  • character "female", text in quotes.
  • logical TRUE / FALSE.
  • factor a category with fixed levels (for example sex, district).

Functions

  • A function takes inputs and returns an output: mean(weights).
  • You name the function, then pass arguments in brackets.
  • summary() describes a whole vector at once.

Packages

  • A package is a bundle of functions. Install once, load each session.
  • install.packages("tidyverse") downloads it (done on your machine).
  • library(tidyverse) loads it for use.

Quiz: install vs load

Recap

  • Data science is a workflow: import, tidy, transform, visualise, communicate, reproducibly.
  • R is the language, RStudio is the workbench, Projects plus here() keep paths sane.
  • Objects (<-), vectors (c()), data types, and functions are the building blocks.
  • Packages add capability: install once, library() each session.
  • Next: best practices, then working with real data.