Source
Write and save .R scripts. Run the current line or selection with Ctrl+Enter, or Cmd+Enter on macOS.
Ageing, health & public statistics
Software 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
Write and save .R scripts. Run the current line or selection with Ctrl+Enter, or Cmd+Enter on macOS.
Shows commands as they execute and prints results, warnings and errors. It is temporary, not the analysis record.
Lists objects currently in memory. Useful for orientation; the script should be able to recreate all of them.
Files, plots, packages, help and the Viewer appear in tabs here.
Screen tour
Read in the cohort
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.
Open gerostats_medical_statistics_r.R in Source. Run the import lines with Cmd+Enter on macOS or Ctrl+Enter on Windows/Linux.
read.csv(..., na.strings = c("", "NA")) creates an object called cohort and converts blank outcome cells to NA.
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
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")
Import, checking, plots, tests, five regression families and confounding.
Download the .R fileA clean project configuration with workspace saving disabled.
Download the .Rproj fileContinue
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.
Official resources
Posit’s official guide to the four panes, a blank slate and Projects.
Open the guideHow project files keep data, scripts and outputs in one working directory.
Open Projects guidanceA concise visual reference for panes, shortcuts and navigation.
Open the PDFThe official R manual for objects, data, factors, tables, models and graphics.
Open the R manual