14/10/2026
Important
Before completing this session, please carefully read about the prerequisites.
| Prerequisite | Description | Where to find it |
|---|---|---|
| R and RStudio installed | Latest versions of both R and RStudio | Download RStudio |
| Accessibility sheet for computers | Fundamentals of computer literacy | Download |
| Basic R skills | Introduction to R - Part I | Access here |
Are there any open questions regarding Introduction to R - Part I?
Which of the following creates an object named my_data containing the number 42?
my_data == 42
my_data <- 42
my_data < - 42
42 <- my_data
How do you access a column named age from a data frame called people_data?
people_data[age]
people_data(age)
people_data$age
age$people_data
What is your level of familiarity with “Packages” in R?
Have you ever imported a dataset (e.g., an Excel, CSV, or SPSS file) into a statistical software program?
How familiar are you with creating data visualizations (e.g., histograms or scatter plots)?
What do we see in the results?
At the end of this session, you will be able to…
Method 1: Using the top menu bar in RStudio
Open RStudio on your own machines. We will use it for later exercises.
Method 2: Using the output pane (bottom right in default RStudio layout)
Verify the current working directory by looking at the top of the console pane (bottom left):
Tip
Alternatively, type getwd() in the console and press Enter to print the exact working directory path.
Setting the working directory
install.packages() function. The package name must be in quotation marks.library() function. Quotation marks are not required here.Packages
This is what you should add to your R script:
characters.rds file use the link below.Setting up the data
Once your working directory is set, you can load your data into RStudio. Check the file extension:
.rds files: R’s native data format. We use the readRDS() function to load them..csv files: The most common text-based data format. Usually loaded with functions from the readr package.haven package you installed earlier!).Point-and-Click Import
You don’t always have to write the code from scratch! You can go to File \(\rightarrow\) Import Dataset in the top menu to point-and-click your way through the import process. RStudio will even generate the correct code for you.
Loading the data
This is what you should add to your R script:
Before analyzing data, you must familiarize yourself with its structure. Built-in functions allow you to verify variables and data types:
names(): Returns the column names.str(): Displays the internal structure (number of rows/columns, data types, and the first few values of each variable).head(): Returns the first six rows of the dataset for a quick preview.summary(): Provides a brief statistical summary of each variable (adapts output based on data type).Codebooks
Variable names are often abbreviated. It is highly advisable to use a codebook for precise definitions. Check out our training material on data dictionaries: https://lmu-osc.github.io/data-documentation-validation-R/02-data-dictionaries/page1.html to learn more!
Inspecting the dataset
Question: Based on the console outputs, can you guess what the data in this dataset is about?
This is what you should add to your R script:
What is the primary purpose of the Working Directory in RStudio?
Which function is used to load an already installed package into your current R session?
install.packages()load.package()open()library()Which command correctly loads an .rds file named characters.rds into an object named my_data?
my_data <- readRDS("characters.rds")readRDS <- my_data("characters.rds")my_data == readRDS("characters.rds")my_data <- read_csv("characters.rds")Which function provides the internal structure of a dataset, including the number of rows/columns and the data types of each variable?
names()head()summary()str()What does the head() function output when applied to a dataset?
What do we see in the results?
As discussed in Part I, we can use indexing tools ($ and []) to access specific parts of our data. In real-world analysis, we apply this to extract entirely new subsets.
$ operator to isolate a single variable (e.g., characters$uni_name).dataframe[condition, ] to select specific rows while keeping all columns.== (checks for exact equality) and | (logical OR, meaning either condition can be true).relevant_data <- ...). Overwriting the original object deletes your raw data from the environment.Subsetting by conditions
Syntax Hint
Remember to specify the data frame for each condition (e.g., data$column == "A" | data$column == "B").
This is an example of what you could have added to your R script:
After isolating the relevant data, we can compute descriptive statistics for specific variables.
mean() or sd() to a single column using the $ operator (e.g., mean(data$column)).table() function to count the frequency of specific text or factor values.dataframe[condition, "column_name"].Comparing group means
Question: Which show features characters with a higher average notability?
Syntax Hint
To compute the mean for a subgroup, place the row condition before the comma, and the target column name after the comma: mean(data[condition, "column_name"]).
This is an example of what you could have added to your R script:
# Basic summary functions
mean(relevant_data$notability)
sd(relevant_data$notability)
# Mean notability for Brooklyn Nine-Nine
mean(relevant_data[relevant_data$uni_name == "Brooklyn Nine-Nine", "notability"])
# Mean notability for Community
mean(relevant_data[relevant_data$uni_name == "Community", "notability"])Beyond calculating numbers, we can visualize our data to gain insights quickly. A great starting point for numeric data is a histogram.
hist() function: We apply this function directly to a numeric vector to generate a plot in the bottom-right Plots pane of RStudio.main for the title and xlab for the x-axis label to make your plots easier to read.Visualizing distributions
Question: By looking at the plots, how do the distributions of the two shows compare?
Side-by-side plots hint
Run par(mfrow = c(1, 2)) before creating your histograms. This tells R to split the plotting window into 1 row and 2 columns so we can easily compare both graphs next to each other!
This is an example of what you could have added to your R script:
# Simple Plotting / Histograms
# Set up the plotting area to show 2 plots side-by-side
par(mfrow = c(1, 2))
# Histogram for Brooklyn Nine-Nine
hist(relevant_data[relevant_data$uni_name == "Brooklyn Nine-Nine", "notability"],
main = "Brooklyn Nine-Nine",
xlab = "Notability"
)
# Histogram for Community
hist(relevant_data[relevant_data$uni_name == "Community", "notability"],
main = "Community",
xlab = "Notability"
)Errors are a normal part of programming. When R throws an error, check for these common culprits first:
Characters is not the same as characters."Community", not Community).notablity instead of notability will cause failures.<-) points to the object name on the left, and check if you accidentally overwrote your data.What is wrong with the following lines of code?
Write your correctd version into your R script.
Some possible corrections of the previous code:
Today, you learned the foundational skills of data handling.
.rds, .csv, Excel). (We did this!)Import \(\rightarrow\) Tidy / (Part I)
Transform \(\rightarrow\) Visualize / (Part II)
Model \(\rightarrow\) Communicate
Building up on your data handling skills in the future …
Sneak peak of writing interactive reports and scientific manuscripts in Quarto for more experienced researchers: https://lmu-osc.github.io/introduction-to-Quarto/
Mastering the basics opens the door to highly advanced, publication-ready tools:
ggplot2 https://ggplot2.tidyverse.org/ allow you to create stunning, highly customized graphicsshiny https://shiny.posit.co/ let you build interactive web applications for your dataLearning R is exactly like learning a new language. It takes time, practice, and patience.
Great job today!
AI tools can hinder or help your learning progress - do not outsource the thinking!
If you do not know what you did, you cannot evaluate or explain the output, defeating the point of credible research.
At the end of this session, you should now be able to…
Create an R script that applies the data handling skills covered today
characters.rds dataset.mean() or table()) on a variable within your subset.hist()) to visualize a numeric variable and customize its title.#) explaining each step.Self-Assessment
Use this assignment to assess your learning of, and progress with, R/RStudio; reflect on any challenges you faced and how you overcame them.
How confident do you feel setting a Working Directory and loading external data into R?
How comfortable do you feel using logical conditions (==, |) to subset data frames?
Compared to the beginning of the workshop, how intimidated do you feel about working with R?
What part of today’s lesson still feels confusing, and which exercise helped your understanding the most?
What do we see in the results?
Creator: Rieger, David (
0009-0004-3701-3101)
Creator: Waterfield, Elizabeth (
0009-0006-3725-6730)
Consultant: Sanchez Steinhagen, Caterina Luz (
0009-0006-4783-1110)
Reviewer: Middleton, Sara (
0000-0001-5307-8029)
Reviewer: von Grebmer zu Wolfsthurn, Sarah (
0000-0002-6413-3895)
This work was originally created by Nicklas Hafiz. His work was subsequently adapted by David Rieger, Elizabeth Waterfield, Caterina Luz Sanchez Steinhagen, Sara Lil Middleton and Sarah von Grebmer zu Wolfsthurn. The current work is licensed under a Creative Commons Attribution 4.0 SA International License (CC-BY-SA 4.0). Code snippets are dedicated to the public domain and are licensed under a Creative Commons Universal Licence (CC0 1.0).
Please cite these materials as: Rieger, D., Waterfield, E., Sanchez-Steinhagen, C. L., Middleton, S., & Von Grebmer Zu Wolfsthurn, S. (2026). Introduction to R: Part II [Quarto slides]. LMU Open Science Center. https://lmu-osc.github.io/teaching-material-for-novices/open-research-tools/r-part-II

LMU Open Science Center