# Run this line only if tidyverse is not already installed. In your R Console.
# install.packages("tidyverse")
# once it is installed you can load it in this notebook with the following line:
library(tidyverse)Lab 1: R and Quarto Foundations
Quarto, packages, data types, vectors, and basic syntax
Learning goals
By the end of this lab, you should be able to:
- explain the purpose of a Quarto notebook and its YAML header;
- distinguish between installing and loading an R package;
- identify common R data types; and
- create vectors and use basic arithmetic and summary functions.
Understanding the YAML header
The YAML header is the block at the very top of this .qmd file, between the two lines containing ---. It provides instructions to Quarto about the document; it is not R code and does not run like a code chunk.
titleandsubtitleset the text shown at the top of the rendered notebook.format: htmltells Quarto to create an HTML document.toc: trueadds a table of contents, andcode-fold: falsekeeps code visible in the output.executecontrols rendering behavior. Here,echo: trueshows R code in the notebook, whilewarning: falseandmessage: falsekeep routine package messages out of the rendered output.
For Markdown conventions, see the official Quarto Markdown Basics guide. For more detail about document metadata, see Quarto’s YAML front matter documentation.
R Syntax
R code uses punctuation to express common operations. The symbols below appear in this lab.
| Symbol | Meaning | Example |
|---|---|---|
<- |
Assigns a value to an object. | quiz_scores <- c(82, 91, 88) |
+, -, *, /, ^ |
Arithmetic: add, subtract, multiply, divide, and raise to a power. | 8 / 2 returns 4; 2 ^ 3 returns 8 |
%%, %/% |
Remainder and whole-number division. | 10 %% 3 returns 1 |
==, !=, <, <=, >, >= |
Compares values and evaluates to TRUE or FALSE. Use == to test equality; do not use = for this. |
quiz_score >= 85 |
&, |, ! |
Logical operators: AND, OR, and NOT. | score >= 85 & hours >= 5 |
= |
Names an argument inside a function call. | mean(x = quiz_scores) |
() |
Calls a function and holds its inputs, called arguments. | mean(quiz_scores) |
, |
Separates arguments inside a function call or values inside c(). |
c(82, 91, 88) |
$ |
Extracts a named column from a tibble/data frame or an element from a list. | students$quiz_score |
|> |
The base R pipe. It passes the result on the left into the next function on the right. | students |> filter(quiz_score >= 85) |
. |
Usually just part of an object or function name, not an operator. | data.frame() |
[] |
Selects one or more elements by position or name. | quiz_scores[1] |
: |
Creates a sequence of whole numbers. | 1:5 |
# |
Starts a comment; R ignores everything after it on that line. | # This is a comment |
{} |
Groups multiple lines of code, often in a function. | function(x) { x + 1 } |
"" |
Creates a character value (text). | "DATA 607" |
L |
Marks a number as an integer. | 4L |
A period is sometimes used as a placeholder in older tidyverse pipe code (%>%). This lab uses the newer base R pipe, |>, so you do not need a period placeholder here.
Start a Quarto notebook
Open this file in RStudio and select Render. Code chunks are the shaded blocks marked with {r}. Run a chunk with the green play button or with Ctrl + Shift + Enter.
Packages: install once, load each session
R packages are collections of functions. You install a package once on a computer, then load it whenever you start a new R session.
tidyverse loads several useful packages, including dplyr for data manipulation and ggplot2 for graphics.
R data types
Create one object for each common data type.
is_enrolled <- TRUE # logical
number_of_courses <- 4L # integer
course_average <- 91.5 # double (a numeric value with decimals)
student_name <- "Avery" # characterUse typeof() and class() to inspect the objects.
typeof(is_enrolled)[1] "logical"
typeof(number_of_courses)[1] "integer"
typeof(course_average)[1] "double"
typeof(student_name)[1] "character"
class(course_average)[1] "numeric"
Practice
Create an object named favorite_tool that contains the name of a data tool as text. Then use typeof() to confirm its type.
# TODO: Write your code here.
favorite_tool <- "RStudio"
typeof(favorite_tool)[1] "character"
Vectors and useful functions
A vector stores multiple values of the same general type.
quiz_scores <- c(82, 91, 88, 95, 76,99,78,88)
length(quiz_scores)[1] 8
mean(quiz_scores)[1] 87.125
min(quiz_scores)[1] 76
max(quiz_scores)[1] 99
Practice
Create a numeric vector named study_hours with five values. Use sum() and mean() to summarize it.
# TODO: Write your code here.
study_hours <- c(2, 3, 4, 5, 6)
sum(study_hours)[1] 20
mean(study_hours)[1] 4
Reflection
In 2–3 sentences, explain the difference between installing a package and loading a package. Then name one R data type you expect to use often. When we install a package, we download it and add it to the computer; most of the time, we use install. packages ().
However, when we load a package with library(), we make its functions available in the current R session; with R data type I, that is often numeric. ## Before you finish
- Save this .qmd file.
- Run all code chunks from top to bottom.
- Render the notebook and confirm that it completes without errors.