Course contents
Part IChapter 1R foundations and data
Orientation: What Are We Learning?
On this page
1. What computers do with code
A computer is a very literal assistant. A program is a sequence of instructions. If you tell R to add two numbers, R will add them; if you tell R to calculate an average, it will calculate an average. R will not understand what a variable means clinically unless you define it correctly.
You already understand the beginning of programming if you have used a spreadsheet. A spreadsheet cell might contain a number or formula. R can store the same numbers, apply the same formula to hundreds of rows, and repeat the operation without dragging cells.
The five words we will use throughout the book are:
| Word | Everyday meaning | Example |
|---|---|---|
| Object | A named box holding information | a box named age |
| Variable | A measured characteristic or a named value | blood pressure |
| Function | A named instruction to do a job | mean() calculates an average |
| Data frame | A table with rows and columns | one row per visit |
| Model | A learned rule connecting inputs to an outcome | risk score built from vitals |
Example: a spreadsheet formula such as =B2/100 becomes a line of R. Here is one height in centimeters, a formula applied to it, and the same formula applied to three heights at once.
height_cm <- 165
height_cm / 100
Output
[1] 1.65heights_cm <- c(165, 180, 152)
heights_cm / 100
Output
[1] 1.65 1.80 1.52The first line prints 1.65. The second applies the same formula to every value and prints 1.65, 1.80 and 1.52, with no dragging of cells.
2. R, RStudio, and Quarto are different
- R is the language and engine executing your code.
- RStudio is an editor with buttons, panes, and shortcuts to make using R easier.
- Quarto turns explanation, code, and outputs into an organized webpage or report.
- GitHub stores versions of source files and supports collaboration. It is not an approved storage location for patient medical records.
Example: suppose you want the mean of three pulse readings. You open RStudio (the editor), type mean(c(70, 82, 90)) in its Console, and R (the engine) calculates the mean and RStudio shows it as 80.66667. If you later want that result in a shareable page, Quarto builds the page from your notes and code. Nothing goes to GitHub unless you choose to save the source files there, and patient records never do.
You can use R without RStudio. You can run the examples without Quarto. But you need R installed in order to execute R code.
3. The story we will follow
A fictional clinic recorded age, sex, BMI, systolic blood pressure, pulse, family history, and a diabetes outcome. Our exercise data are explicitly synthetic, meaning every row was fabricated by a computer formula, not copied from an actual medical chart.
We will ask: Can these measurements help predict the synthetic outcome for new examples?
This is a training question, not a recommendation for real patients. By the end, you will understand why a high accuracy score alone does not justify clinical use.
Example: a made-up row could read: a 54-year-old woman with BMI 27.2 and systolic pressure 138. The question is whether rows like this, taken together, let us guess the diabetes label for a new row. The answer from a training exercise tells you about the fabricated formula, not about any real person.
How the course is organized. It has six parts. Part I (chapters 1 to 8) teaches R and data handling. Part II (chapters 9 to 18) carries one supervised prediction question through the whole workflow, ending in a capstone. Part III (chapters 19 to 22) adds more supervised methods. Part IV (chapters 23 to 26) covers unsupervised learning, where there is no outcome column. Part V (chapters 27 and 28) covers other kinds of learning, such as incomplete labels. Part VI (chapters 29 to 31) helps you choose and compare methods, ending in a capstone. Read in order; each part assumes the ones before it.
4. How to study a programming book
For each example: (1) read it, (2) predict what it does, (3) run it, (4) compare actual and expected results, (5) change one part, and (6) explain it to someone. Errors are informative messages; do not hide or ignore them.
A code chunk is a gray block labeled R. Put your cursor on a line and use Ctrl+Enter on Windows or Cmd+Enter on Mac to run it in RStudio. A chunk with three lines runs those instructions in order.
# This line is a comment: it explains the code but does not run.
2 + 3
Output
[1] 5Expected result: 5. The [1] printed before a value in the Console is R’s index notation; it is not part of your answer.
5. Your first healthcare-data rule
Never add a real patient name, MRN, phone number, address, or clinical note to this course or to your practice files. Do not paste protected or potentially identifying data into an issue, AI tool, or external spreadsheet. Never put real patient information in your practice files. Keeping a file private does not remove regulatory or organizational restrictions.
Example: a colleague emails a spreadsheet with a column of names and asks you to “just run a quick check in R”. Following the rule, you do not open it in the course project. You ask for a version with the names and record numbers removed, or you use the synthetic file in data/ to practice the same steps. A practice file that contains Maria Lopez, MRN 004512 breaks the rule; one that contains patient_id P0007 and a fabricated age does not.
Try it
- Explain, in your own words, what the difference is between R and RStudio.
Show solution to question 1
Answer: R is the language and engine that runs your code; RStudio is the editor around it, with panes, buttons and shortcuts.
Step by step
- Ask which part does the calculating. That is R: it is installed separately and can run without RStudio.
- Ask what only makes R easier to use. That is RStudio: it has a script editor, a Console, a place to see objects and plots, but it cannot run code without R.
- A car comparison helps: R is the engine, RStudio is the dashboard and steering wheel.
- Run
10 + 5. Change the operator to*and predict the result before running.
Show solution to question 2
Answer: 10 + 5 is 15 and 10 * 5 is 50.
Step by step
- Run the sum.
10 + 5Output
[1] 152. R prints [1] 15. The [1] is only the position marker of the first value.
3. Predict the product: ten groups of five is 50. Then check.
10 * 5Output
[1] 504. The output is 50, matching the prediction. If it did not match, that is a clue to find what you misread.
- Name one reason an apparently accurate ML model might still be unsafe for medical decisions.
Show solution to question 3
Answer: A high accuracy score can come from leakage, biased labels, missing groups or an inappropriate endpoint, so the model can look good and still fail real patients.
Step by step
- Accuracy only compares predictions with the recorded labels in one dataset.
- If the labels are wrong or biased (for example, a diagnosis recorded only for people who got tested), the model learns that bias.
- If a group of patients is missing from the data, the score says nothing about them.
- A fuller example: a model that predicts “no disease” for everyone is 95% accurate when 5% of patients have the disease, yet it finds none of them.
Checkpoint
You can distinguish the program, editor, report generator, and GitHub; you can run a line of R; and you know that all course examples are synthetic.