[1] 3
2 Introduction to R, RStudio, and Quarto
2.1 Learning Objectives
By the end of this week, you should be able to:
- Install and open R, RStudio, and Quarto
- Navigate the four-pane layout of RStudio
- Create and run R scripts
- Understand the differences between the console, script editor, and environment
- Execute basic R operations and understand data types
- Install and load R packages
- Create and render a Quarto (
.qmd) document to.pdf
2.2 Getting Started
R is a programming language designed for data analysis.
RStudio is an Integrated Development Environment (IDE) that makes working with R easier.
Quarto is a tool for creating reproducible documents that combine code and text.
2.3 Installing R, RStudio, and Quarto
- Install R: https://cran.r-project.org/
- Install RStudio: https://posit.co/download/rstudio-desktop/
- Install Quarto: https://quarto.org/docs/get-started/
When you open RStudio, you’ll see four panes:
- Console – runs code interactively
- Source – write and save scripts or Quarto documents
- Environment/History – view and manage objects
- Files/Plots/Packages/Help/Viewer – navigation and visualization tools
2.4 Introduction to Quarto
Quarto allows you to create documents that include both text and executable R code.
2.4.1 Your First Quarto Document
- In RStudio: File → New File → Quarto Document
- Replace the header with:
---
title: "My First Quarto Document"
author: "Your Name"
format: pdf
---
- Below the header, add:
- Click Render to produce a PDF file.
2.4.2 In-Class Quarto Exercise
- Create a new Quarto document with:
- A title, your name, and the date
- A short paragraph of text
- A code chunk that calculates the mean and standard deviation of a numeric vector
- Render it to PDF and verify it works.
2.5 Basic R Concepts
2.5.1 Variables and Assignments
x <- 5
y <- 10
z <- x + y
z[1] 15
2.5.2 Vectors and Functions
2.5.3 Data Frames
name <- c("Alice", "Bob", "Charlie")
age <- c(25, 30, 35)
student_data <- data.frame(name, age)
student_data name age
1 Alice 25
2 Bob 30
3 Charlie 35
2.5.4 Inspecting Data
str(student_data)'data.frame': 3 obs. of 2 variables:
$ name: chr "Alice" "Bob" "Charlie"
$ age : num 25 30 35
summary(student_data) name age
Length:3 Min. :25.0
Class :character 1st Qu.:27.5
Mode :character Median :30.0
Mean :30.0
3rd Qu.:32.5
Max. :35.0
head(student_data) name age
1 Alice 25
2 Bob 30
3 Charlie 35
2.5.6 Using Scripts and Console
- Write your code in the script editor and run lines with Ctrl+Enter (Cmd+Enter on Mac)
- Save scripts with the
.Rextension - Use the Console for quick exploration
2.5.7 Installing and Loading Packages
install.packages("tidyverse")2.5.8 In-Class R Exercises
- Create a numeric vector of five numbers and calculate its mean, median, and standard deviation.
- Create a data frame with three columns (name, age, and major) and print its structure.
- Import a dataset from a URL using
read.csv()and summarize it usingsummary().
[1] 30
median(my_vec)[1] 30
sd(my_vec)[1] 15.81139
df <- data.frame(
name = c("Lily", "Mark", "Tom"),
age = c(21, 22, 23),
major = c("Biology", "Math", "History")
)
str(df)'data.frame': 3 obs. of 3 variables:
$ name : chr "Lily" "Mark" "Tom"
$ age : num 21 22 23
$ major: chr "Biology" "Math" "History"
Month X1958 X1959 X1960
Length:12 Min. :310.0 Min. :342.0 Min. :390.0
Class :character 1st Qu.:339.2 1st Qu.:387.5 1st Qu.:418.5
Mode :character Median :360.5 Median :406.5 Median :461.0
Mean :381.0 Mean :428.3 Mean :476.2
3rd Qu.:411.8 3rd Qu.:465.2 3rd Qu.:514.8
Max. :505.0 Max. :559.0 Max. :622.0
2.6 Homework Preview
- Create a
.qmddocument that: - Render to PDF and submit to Canvas.
2.7 Next Steps
You now know how to run R scripts and render Quarto documents.
Next week, you’ll learn how to create data visualizations using ggplot2.
2.5.5 Comments and Help