Software first contact

RStudio first contact.

A practical introduction to RStudio Projects, the four panes and the first reproducible commands for medical data.

R is the statistical language; RStudio is an environment for writing, running and organising R code.

The workspace

Know where each part of the work lives.

Source

Write and save .R scripts. Run the current line or selection with Ctrl+Enter, or Cmd+Enter on macOS.

Console

Shows commands as they execute and prints results, warnings and errors. It is temporary, not the analysis record.

Environment

Lists objects currently in memory. Useful for orientation; the script should be able to recreate all of them.

Output

Files, plots, packages, help and the Viewer appear in tabs here.

Screen tour

See where the work happens.

RStudio interface labelled with Source, Console, Environment and Output panes
Work moves clockwise: write code in Source, watch it run in the Console, inspect created objects in Environment and review files, plots and help in Output. The script—not the Environment pane—must be able to recreate the analysis. Posit RStudio User Guide ↗

Read in the cohort

From download to checked data.

  1. 01

    Keep the project together

    Put the .Rproj, .R script and cohort CSV in one folder, then open the project file. The project becomes the working directory, so the script does not need a computer-specific path.

  2. 02

    Open the source script

    Open gerostats_medical_statistics_r.R in Source. Run the import lines with Cmd+Enter on macOS or Ctrl+Enter on Windows/Linux.

  3. 03

    Read blank cells as missing

    read.csv(..., na.strings = c("", "NA")) creates an object called cohort and converts blank outcome cells to NA.

  4. 04

    Check before modelling

    dim(cohort) should return 720 rows and 16 columns. Use str(), head() and missing-value counts to check types and content.

Clinical sense-check

The numeric code 2 in frailty_cat_6m means “frail”; it does not mean twice as frail as category 1. Create an ordered factor for tables while preserving the original numeric field for an auditable record.

Working habit

Keep the analysis in syntax.

Create one RStudio Project for the analysis and keep the CSV, script and outputs inside it. Turn off automatic workspace saving and start each session from a blank slate; this exposes any step missing from the script.

Open the supplied .Rproj, then open the .R file in the Source pane. Run one numbered section at a time.

# A hash begins a comment.

cohort <- read.csv(
  "gerostats_medication_review_cohort.csv",
  na.strings = c("", "NA")
)

str(cohort)
dim(cohort)
summary(cohort$age_years)
table(cohort$med_review, useNA = "ifany")

Complete R script

Import, checking, plots, tests, five regression families and confounding.

Download the .R file

Continue

Use the common cohort.

The Statistics Lab uses one synthetic older-adult cohort in Stata, SPSS and R. Start with the import and checking page, then follow the same statistical route in your chosen software.

Open step 01 · Meet the cohort

Official resources

Documentation and videos.

Hello RStudio

Posit’s official guide to the four panes, a blank slate and Projects.

Open the guide

RStudio Projects

How project files keep data, scripts and outputs in one working directory.

Open Projects guidance

RStudio IDE cheatsheet

A concise visual reference for panes, shortcuts and navigation.

Open the PDF

An Introduction to R

The official R manual for objects, data, factors, tables, models and graphics.

Open the R manual