On this site
There are only two kinds of programming languages: the ones people complain about and the ones nobody uses. —Bjarne Stroustrup
Welcome

Welcome to R for Biochemists @ UAM, the R section of the course Coding Tools for Biochemistry and Molecular Biology (Herramientas de Programación para Bioquímica y Biología Molecular, HPBBM), taught in the Bachelor’s Degree in Biochemistry at Universidad Autónoma de Madrid.
The aim of these materials is not only to introduce the syntax of R, but also to help you use programming as a practical tool for data handling, reproducible analysis and scientific problem solving in biochemistry and molecular biology.
Throughout the course, we will work with examples and datasets inspired by biological questions, including experimental measurements, tabular biological data, sequence-related information and simple reproducible reports. The goal is to connect programming concepts with real scientific workflows.
These materials may be updated during the course. Small corrections, new examples or additional exercises can be added as the course progresses.
This website contains teaching materials for the R section of HPBBM. It is intended as an open and interactive learning resource, but it does not replace the official course information. Detailed academic information about the course, including official contents, calendar, assessment criteria, announcements and course-specific instructions, is available through the UAM Moodle site. The official course syllabus is also available from the UAM course information system and as a PDF version available here
1 What you will learn
By working through these lessons, you should become familiar with:
- Basic R syntax, variables, operators and data structures.
- Conditional statements and loops.
- Reading and writing data files.
- Working with data frames and biological datasets.
- Writing and using functions.
- Installing and loading R packages.
- Managing, transforming and summarising data.
- Creating reproducible documents with RStudio and Quarto.
- Applying programming concepts to biochemical and molecular biology problems.
This is not intended to be a general programming course detached from biology. Instead, the main objective is to help you see R as a useful tool for your own scientific work, starting with your TFG.
The R materials are organised as a sequence of lessons. Although you can revisit individual pages whenever you need them, we recommend following the lessons in order if you are new to R:
- R basics: variables, operators, expressions, vectors, factors and basic data structures.
- Flow control: conditional statements,
if,else, loops and control statements. - Reading and writing data: working directories, file paths, reading tables and writing output files.
- Functions and packages: built-in functions, function arguments, CRAN, Bioconductor and package-related problems.
- Data visualisation: basic plots, figure customisation and graphical representation of data.
- Advanced data management: sorting, filtering, selecting, transforming and aggregating datasets.
- Reproducible documents: RStudio Projects, Markdown, Quarto, code chunks and reproducible reports.
- Bioinformatics resources in R: biological datasets, bioinformatics-oriented packages and practical examples related to molecular biology and biochemistry.
2 How to use this website
These materials have been designed as an active learning resource. You will get much more out of the course if you use the website interactively rather than reading it passively.
We suggest the following workflow:
Read the explanations carefully. Try to understand what each piece of code is doing before running it.
Run the code yourself. Copy the examples into your own R session or script and check whether you obtain the same output.
Modify the examples. Small changes are often the best way to understand how code works.
Try the quick exercises. These are short questions intended to check your understanding as you progress through each lesson. We included several new exercises for year 2026-27, thus any feedback on this would be apreciated.
Work on the review exercises. These exercises are designed for deeper practice and classroom discussion.
Ask for help when needed. Programming involves errors, warnings and unexpected results. Learning how to understand and fix them is part of the course.
Whenever you see a block of R code, pause for a moment and ask yourself:
- What object is being created?
- What function is being used?
- What input does the function receive?
- What output do I expect?
Then run the code and compare your prediction with the actual result.
The lessons include different kinds of exercises:
Quick exercises are short interactive questions embedded in the lessons. They are intended to help you check whether you understood the previous concept before moving on. These are usually interactive exercises created with WebR or webexercises packages. When available, these exercises allow you to test your understanding directly in the browser.
Extra exercises are suggested extra tasks that require you to write or adapt code, usually from other R courses.
Review exercises are longer exercises intended for independent previous work and classroom discussion.
3 Before you start: software, data and repository
Before working through the R lessons, please make sure that you have:
- A recent version of R installed.
- A recent version of RStudio installed.
- Access to the course data folder.
- A working folder or, preferably, an RStudio Project for the course.
- Basic familiarity with files and folders in your operating system.
If you installed R or RStudio some time ago, like in your first University year, please update both before continuing. Some examples and packages used in the course may require recent versions.
Each lesson includes a final R session info section showing the R version and package versions used to render the page. This information can help you identify version-related issues if something does not work as expected on your computer.
The source files for this website are available in the course repository, containing the source files and the data used in the examples and exercises. In particular, it includes:
- The Quarto source files for the lessons, such as
index.qmd,r1.qmd,r2.qmd, etc. - The course configuration file,
_quarto.yml. - The datasets used in examples and exercises, mainly in the
data/folder. - Supporting files used by the website, such as images, styles and extensions.
- Additional material related to exercises and course examples.
- The files used to generate the published GitHub Pages website.
Several lessons use example datasets stored in the course repository. These files are needed for exercises involving reading, writing and analysing data. The icon on the top-right corner in all the site pages will allow you to download the repo data folder (link generated with DownGit, note that your browser can consider it as dangerous and you may need to change security options). Note that this folder can be updated during the course.
A recommended folder structure for your own work is:
HPBBM/
├── data/
├── scripts/
├── results/
└── notes/
Keeping your files organised from the beginning will make your work easier, more reproducible and less error-prone.
Whenever possible, keep the original data files unchanged. Save scripts, processed files and results in separate folders.
4 Quarto, .qmd files and reproducible documents
This website is built with Quarto, a publishing system that allows us to combine text, code, results, figures and references in the same source document.
Most pages in this site are generated from .qmd files. A .qmd file is a plain-text file that can contain:
- Markdown-formatted text.
- R code chunks.
- Code output.
- Figures and tables.
- Cross-references.
- Callout boxes.
- Links and bibliographic references.
- Rendering options for HTML output.
This is useful for teaching programming because the explanation, the code and the output remain connected. When the source file is rendered, Quarto executes the code chunks and generates the final HTML page.
Using Quarto helps make the materials more reproducible. The same source file contains the explanation, the code and the output, making it easier to update, check and reuse the course materials.
If you want to understand how the pages are generated, see lesson R7, where we introduce Markdown, Quarto and reproducible documents in RStudio.
5 Asking for help and using AI tools responsibly
Getting stuck is normal when learning to program. The most useful questions are those that describe the problem clearly.
When you ask for help, try to include:
- What you wanted to do.
- The code you tried.
- The error message or unexpected result you obtained.
- What you think the problem might be.
- What you have already tried to fix it.
For example, instead of saying:
That does not work.
Try to say:
I tried to read a CSV file using
read.csv(), but R says that the file does not exist. I checked the file name, but I am not sure whether my working directory is correct.
This kind of question makes it much easier to identify the problem and helps you develop debugging skills.
AI tools can also be a very useful resource when learning programming. They can help you move faster, understand errors, compare alternative solutions and explore new ways of approaching a problem. In that sense, using AI in programming is a great opportunity.
However, AI tools should support your learning, not replace it.
At the beginning, it is especially important that you try to solve problems by yourself before asking an AI tool for help. Programming is learned by thinking, testing, making mistakes, debugging and trying again. If you skip that process too early, you may obtain a working answer without really understanding it.
A good way to use AI is to ask specific questions about a concrete difficulty. For example:
- Why am I getting this error message?
- What does this function do?
- Can you explain the difference between these two approaches?
- Can you give me a simpler example of this concept?
- Can you help me debug this code without giving me the full solution?
A less useful way to use AI is to ask directly for the complete solution to an exercise before making a serious attempt yourself.
Before asking an AI tool for help, try to write your own code, even if it is incomplete or incorrect. Then use the AI tool to understand what is wrong, improve your reasoning or compare different approaches.
During the course, we will also use AI-generated solutions as learning material. When correcting exercises, we will compare our own solutions with solutions proposed by AI tools. This will help us discuss their advantages, but also their limitations, errors and possible misunderstandings.
The objective is not to reject AI, but to use it critically.
AI use in course projects
AI can help us learn more and learn faster, but only if we use it with judgement. It is useful when it helps us understand better, think more clearly and solve problems more effectively. It is not useful if it simply replaces our own reasoning.
For the course projects, the use of AI tools is allowed, but students are expected to understand all the code they submit.
You (meaning your group) may use AI tools to help you write, debug or improve code, but you are responsible for understanding, checking and explaining the final version that you submit.
During review and feedback sessions, we may ask you to explain your code, justify your choices and describe how the different parts of your project work. If important parts of the code have been generated by AI and you cannot explain them, this will be penalised.
In this course, the goal is not only to obtain code that works. The goal is to understand what the code does, why it works, and how it can be adapted to disclose new biological or biochemical questions.
6 License, reuse and feedback
The teaching materials are described here as CC BY-NC. If the GitHub repository contains a separate software license file, please check whether it applies to the source code, website infrastructure or the whole repository, and make sure that the intended licensing is unambiguous.
Additional references, manuals and online resources are provided throughout the lessons. You are encouraged to consult them when you need alternative explanations, more examples or further practice.
7 Bibliography and Resources
Each lesson contains its own specific references, but we wanted to highlight here some curated general open access and open source resources to introduce yourself into R and R Studio.
- Grolemund, G. Hands-on Programming in R.
- Peng, R. D. R Programming for Data Science.
- Coulatti, R. I. R Crash Course for Biologist.
- Chang, W. R. R Graphics Cookbook.
- Doyle, M., Chung, J. & Perreau V. Introduction to R for Biologists.
- Learn R by example
- R base cheatsheet
- R programming 101 @YouTube
- Higgins, P. D. R. Reproducible Medical Research with R.
Footnotes
See the code in the script r_sticker.R in the repo.↩︎