Section 1 of 9

1 Before you start

Before you begin, take a few minutes to check what you already know and how confident you feel. You will see the same questions again at the end of the module — this helps both you and us measure what you have learned. Click an option for every question and confidence rating, then click Next to continue.

Pre-test

When you run an analysis on your own machine, why do R users work inside an RStudio Project?

Pre-test

Which line points to your data in a way that still works when a colleague re-runs the analysis on their own computer?

Pre-test

Which line stores the number 54 in an object called age?

Pre-test

You type x <- 5 but accidentally write x < -5 instead. What does x < -5 actually do?

Pre-test

You want one object holding four patient ages — 54, 61, 47, 73. Which line builds it?

Pre-test

You have ages <- c(54, 61, 47, 73). Which line returns the second patient's age, 61?

Pre-test

A variable records each patient's sex as "M" or "F", with no other values allowed. Which type best represents it in R?

Pre-test

For hba1c <- c(7.2, 8.1, 6.5, 9.4), what does sum(hba1c > 8) return?

Pre-confidence

I can create and name R objects with <-, and build and summarise a vector of clinical values with c().

Not at all confident
Fully confident
Pre-confidence

I can pull values out of a vector by position with square brackets, remembering that R counts from 1.

Not at all confident
Fully confident
Pre-confidence

I can tell whether a variable should be numeric, character, logical, or a factor, and explain when a category belongs in a factor.

Not at all confident
Fully confident
Section 2 of 9

2 Introduction

R is the language working biostatisticians use to clean data, run analyses, and make the figures and tables you see in clinical papers. Before you can analyse anything, you need somewhere to put the data and a way to talk about it — a named value, a column of results, and the type each value carries. This first part builds exactly that vocabulary, the groundwork everything else in the course is built on.

This part of the module covers four foundations you will reach for in every analysis that follows:

  • Workbench — how R, RStudio, and an RStudio Project fit together, and why a relative path keeps your analysis reproducible on any machine.
  • Objects — storing a value under a name with the assignment arrow <-, so you can use it again later.
  • Vectors — holding many values, such as a whole column of ages or lab results, in one object with c(), and pulling values back out by position.
  • Data types — telling apart the numeric, character, logical, and factor values that clinical data is made of, and when a category belongs in a factor.

By the end of this part you will be able to create and name R objects with the assignment arrow, build and summarise a vector of clinical values, pull out values by position, and identify what type each variable is — including when a category should be a factor.

A clinical dataset is built up from simple parts, a named value (object) becomes a column (vector) of one particular type, so learning objects, vectors, and types first lets everything later fall into place.
A clinical dataset is built up from simple parts, a named value (object) becomes a column (vector) of one particular type, so learning objects, vectors, and types first lets everything later fall into place.

Try every snippet in the R Scratchpad on the right. This part builds its own small vectors by hand with c() and factor(), so it needs no data file — though diabetes_clinic.csv is already loaded and waiting for the next part.

Section 3 of 9

3 R, RStudio, and why you work inside a Project

R is a programming language built for data and statistics. RStudio — made by a company called Posit — is the workbench you run R inside: an editor, a console, and panes for your plots and data, all in one window. You write R; RStudio makes it comfortable. In this lesson the workbench is your browser, and the data is already loaded, so you can focus on the language.

R is the programming language you actually write, while RStudio is the workbench (an editor, a console, and panes for your data and plots) that you run that R inside.
R is the programming language you actually write, while RStudio is the workbench (an editor, a console, and panes for your data and plots) that you run that R inside.

When you start a real piece of analysis on your own machine, you make an RStudio Project: a single folder that holds everything for that analysis — the data, your code, and the outputs. Working inside a Project means R always knows where your files are, so the same analysis runs the same way on your laptop, a colleague's machine, or on cloud.

An RStudio Project bundles a single analysis (its data, code, and outputs) into one self-contained folder, so the exact same analysis reproduces identically on any machine.
An RStudio Project bundles a single analysis (its data, code, and outputs) into one self-contained folder, so the exact same analysis reproduces identically on any machine.

Start one habit right now: always remember to point to your data with a relative path like data/diabetes_clinic.csv, not an absolute path like C:/Users/you/Desktop/diabetes_clinic.csv. An absolute path breaks the moment the analysis runs on a different computer — and in clinical work, your analysis will be re-run by someone else.

An absolute path is tied to one computer and breaks the moment your code runs somewhere else, while a relative path travels inside the project folder so it keeps working when a colleague re-runs your analysis.
An absolute path is tied to one computer and breaks the moment your code runs somewhere else, while a relative path travels inside the project folder so it keeps working when a colleague re-runs your analysis.
Section 4 of 9

4 Objects and the assignment arrow

An object is a name you give to a value so you can use it again later. You create one with the assignment arrow <-: the name on the left, the value on the right.

age <- 54 stores the number 54 under the name age. From then on, typing age gives you 54 back, and you can compute with it — age + 1 is 55. Good object names are short but say what they hold: age, hba1c, n_patients.

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
age <- 54
weight_kg <- 78.5
age
weight_kg
An object is a name bound to a value with <-; once stored, the name stands in for that value so you can read it and compute with it, and using it does not change it.
An object is a name bound to a value with <-; once stored, the name stands in for that value so you can read it and compute with it, and using it does not change it.

You will also see = used for assignment. Both work, but R users write <- to assign objects and keep = for setting options inside a function. Pick <- and stay consistent.

Watch the spaces around <-. x <- 5 assigns 5 to x, but x < -5 asks whether x is less than negative five — a completely different question. When in doubt, put a space on each side of <-.

Section 5 of 9

5 Vectors: the workhorse of clinical data

Clinical data rarely arrives one value at a time — it arrives as a column: every patient's age, or a run of lab results. A vector is a single object that holds many values of the same type, and you build one with c() (think c for combine).

ages <- c(54, 61, 47, 73) stores four ages in one object. A column in a dataset is just a vector — which is why vectors are the foundation everything else stands on.

R works on the whole vector at once. ages + 1 adds a year to every age; mean(ages) averages all four. You never loop over patients one by one.

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
hba1c <- c(7.2, 8.1, 6.5, 9.4)
hba1c
mean(hba1c)
length(hba1c)
A vector is a single object holding many values of one type, and R applies an operation to every element of it at once rather than looping through them one at a time.
A vector is a single object holding many values of one type, and R applies an operation to every element of it at once rather than looping through them one at a time.

To pull out one value, use square brackets with its position: ages[1] is the first age, ages[4] the fourth.

If you have used another language such as Python, note that R counts from 1, not 0: ages[1] is the first element, not the second.

Now do the indexing yourself. Square brackets also take several positions at once — hand them a vector of positions built with c().

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
ages <- c(54, 61, 47, 73)
ages[1]
ages[4]
ages[c(2, 3)]
Square brackets subset a vector by position, R numbers those positions from 1, and you can ask for one element or several at once by passing a vector of positions with c().
Square brackets subset a vector by position, R numbers those positions from 1, and you can ask for one element or several at once by passing a vector of positions with c().

A common first slip is leaving out c() and just separating values with commas. R needs the c() to know you mean one vector of several values.

Debug & fix

The code below is broken. Type a fixed version into the editor, then click Run & check. Success means your code runs without errors. Use Show hint only if you get stuck.

Broken code (do not copy verbatim)
ages <- 54, 61, 47
Section 6 of 9

6 The four core data types

Every value in R has a type, and clinical data leans on four of them. The type matters because it decides what you can do with a variable: you can average ages, but not diagnoses.

  • numeric — numbers, whole or decimal: an age of 54, an HbA1c of 7.2, a p-value of 4.2e-8.
  • character — text, always in quotes: a gene name "APOE", a patient ID "D001", a treatment label "metformin".
  • logicalTRUE or FALSE, the answer to a yes/no question: hba1c > 8 is TRUE for a result above eight.
  • factor — a category with a fixed set of allowed values: a treatment arm that can only be metformin, insulin, or lifestyle; a disease stage; a sex.
Every value in R carries a type (numeric, character, logical, or factor), and that type, not the value itself, governs which operations are legal, which is why a factor's fixed set of allowed levels matters.
Every value in R carries a type (numeric, character, logical, or factor), and that type, not the value itself, governs which operations are legal, which is why a factor's fixed set of allowed levels matters.

You can always ask R what type a value is with class().

Logical values appear the moment you ask a question of your data. Compare a whole vector against a threshold and R answers for every element at once, handing back a logical vector you can then count with sum() — how many results are above eight?

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
hba1c <- c(7.2, 8.1, 6.5, 9.4)
hba1c > 8
class(hba1c > 8)
sum(hba1c > 8)
Comparing a vector to a threshold returns a TRUE/FALSE for every element (a logical vector), and because TRUE equals 1, sum() of that vector counts how many elements pass the test.
Comparing a vector to a threshold returns a TRUE/FALSE for every element (a logical vector), and because TRUE equals 1, sum() of that vector counts how many elements pass the test.
Section 6.1 of 9

6.1 Factors: categories R can count and order

A factor is R's type for a categorical variable — one that takes a small, fixed set of values called its levels. Storing treatment as a factor, rather than plain text, lets R summarise it, count each group, and — later — use it correctly in a statistical model.

You turn text into a factor with factor(), and you see its categories with levels().

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
treatment <- factor(c("metformin", "insulin", "lifestyle", "metformin"))
treatment
levels(treatment)
A factor is text reorganised into a small fixed set of distinct levels, so repeated values collapse to the same category that R can then count and model.
A factor is text reorganised into a small fixed set of distinct levels, so repeated values collapse to the same category that R can then count and model.

Some categories carry a meaningful order — disease stage runs I < II < III < IV. You can tell R that order, so stages tabulate and plot in clinical sequence instead of alphabetically.

A factor also carries a reference level — the category every other group is compared against in a model. You will set that deliberately in the next module; for now, just remember that a category usually belongs in a factor, not loose text.

Storing an ordered category as a factor makes R respect its real-world clinical sequence in tables and plots, instead of falling back to alphabetical order.
Storing an ordered category as a factor makes R respect its real-world clinical sequence in tables and plots, instead of falling back to alphabetical order.
Section 7 of 9

7 Check your understanding

You have reached the end of the module. Try the same questions again — your answers here, paired with your pre-test answers, are how we measure what the module taught you. Answer every question and confidence rating, then click Submit and see results to view your score.

Post-test

When you run an analysis on your own machine, why do R users work inside an RStudio Project?

Post-test

Which line points to your data in a way that still works when a colleague re-runs the analysis on their own computer?

Post-test

Which line stores the number 54 in an object called age?

Post-test

You type x <- 5 but accidentally write x < -5 instead. What does x < -5 actually do?

Post-test

You want one object holding four patient ages — 54, 61, 47, 73. Which line builds it?

Post-test

You have ages <- c(54, 61, 47, 73). Which line returns the second patient's age, 61?

Post-test

A variable records each patient's sex as "M" or "F", with no other values allowed. Which type best represents it in R?

Post-test

For hba1c <- c(7.2, 8.1, 6.5, 9.4), what does sum(hba1c > 8) return?

Post-confidence

I can create and name R objects with <-, and build and summarise a vector of clinical values with c().

Not at all confident
Fully confident
Post-confidence

I can pull values out of a vector by position with square brackets, remembering that R counts from 1.

Not at all confident
Fully confident
Post-confidence

I can tell whether a variable should be numeric, character, logical, or a factor, and explain when a category belongs in a factor.

Not at all confident
Fully confident
Section 8 of 9

8 Your results

Here is how your post-test answers compare with your pre-test answers. The pre/post pairing is the most reliable way to see what this module actually taught you.

Your score

Submit the post-test to see your results.

Muddiest point

What is the one thing from this module that is still unclear to you?

Rate this module

Overall, how would you rate this module?

How likely are you to recommend this module to a peer? (0 = not at all, 10 = extremely likely)