UC San Diego
COGS 137 - Fall 2026
2026-09-30
Q: What are some other important functions in R that we should keep in mind?
We’ll get to these today and Monday! The most important will be the verbs in the tidyverse - on Monday!
Q: Does Quarto take in / print code? Or is it primarily used just for text-based content?
It will display code as well as execute it on render. It’s for text and code.
Q: I’m curious about how we’ll be able to manipulate datasets using R, or if this is something we will end up doing. I wonder how using R will compare to using excel.
In lots of ways, Excel can do many of the things you can do with data in R; however, R has the advantage of being reproducible. This means you write the code once, but can execute it as many times as you want. This is unlike Excel, where each time you want to do something, you have to point/click to make it happen. And, there’s not a record of what’s been done, so being able to reproduce what someone else has done is nontrivial.
Q: I think I was confused as to the use of Quarto when we were running code on the console of Rstudio but I understood how Quarto is used as a source to save code, but im curious as to why the console was not made to save it.
Technically you can save your console, but for our purposes we’re not going to because it’s not particularly readable. Think of the console as a place to try things out, but anything you really want to save/keep/organize should go in a (quarto) document
Due Dates:
Variables are how we store information so that we can access it later.
Variables are created and stored using the assignment operator <-
The above stores the value 3 in the variable first_variable
Note: Other programming languages use = for assignment. R also uses that for assignment, but it is more typical to see <- in R code, so we’ll stick with that.
| Variable Type | Explanation | Example |
|---|---|---|
| character | stores a string | "cogs137", "hi!" |
| numeric | stores whole numbers and decimals | 9, 9.29 |
| integer | specifies integer | 9L (the L specifies this is an integer) |
| logical | Booleans | TRUE, FALSE |
| list | store multiple elements | list(7, "a", TRUE) |
Note: There are many more. We’ll get to some but not all in this course.
logical - Boolean values TRUE and FALSE
double - floating point numerical values (default numerical type)
The most basic data structure in R is the vector: an ordered collection of values that are all the same type.
[1] 1 2 3
[1] 1 2 3 4 5
[1] 3
[1] "character"
A single value like 3 is just a vector of length 1.
So far, every variable has been an atomic vector: it can hold many values, but they must all be the same type.
Define variables of each of the following types: character, numeric, integer, logical, list
class() (and View() & median()) were our first functions…but we’ll show a few more.Functions are:
A vector can only hold one type. If you combine different types, R coerces (converts) them to a common type – without any warning.
(This is separate from dynamic typing, which is about how a variable gets its type.)
R converts to the most flexible type needed:
logical → integer → double → character
R uses NA to represent missing values in its data structures.
NaN | Not a number
Inf | Positive infinity
-Inf | Negative infinity
At its simplest, R is a calculator. To carry out mathematical operations, R uses operators.
| Operator | Description |
|---|---|
+ |
addition |
- |
subtraction |
* |
multiplication |
/ |
division |
^ or ** |
exponentiation |
x %% y |
modulus (x mod y) 9%%2 is 1 |
x %/% y |
integer division 9%/%2 is 4 |
Output can be stored to a variable
These operators return a Boolean.
| Operator | Description |
|---|---|
< |
less than |
<= |
less than or equal to |
> |
greater than |
>= |
greater than or equal to |
== |
exactly equal to |
!= |
not equal to |
Combine (or negate) TRUE/FALSE values. You’ll use these constantly to filter data.
& and: TRUE only if both sides are TRUE| or: TRUE if either side is TRUE! not: flips TRUE ↔︎ FALSEmy_age.days_old.days_old is between 7,000 and 10,000.install.packages()library()Think of it like an app: you install it once, but you open it every time you want to use it.
In this course, most packages we’ll use have been installed for you already on datahub, so you will only have to load the package in (using library).
Question
Should install.packages() go in your Quarto document or in the console? Why?
Hint: Think back to our discussion on the first day about what the console is for vs. what a Quarto document is for…and what happens every time you render.
“set” is in quotation marks because it is not a formal data class
A tidy data “set” can be one of the following types:
tibbledata.frameWe’ll often work with tibbles:
readr package (e.g. read_csv function) loads data as a tibble by defaulttibbles are part of the tidyverse, so they work well with other packages we are usingA data frame is the most commonly used data structure in R, they are list of equal length vectors (usually atomic, but can be generic). Each vector is treated as a column and elements of the vectors as rows.
A tibble is a type of data frame that … makes your life (i.e. data analysis) easier.
Most often a data frame will be constructed by reading in from a file, but we can create them from scratch.
Columns (variables) in data frames are accessed with $:
Data stored in columns can include different kinds of information…which would require a different type (class) of variable to be used in R.

R Data Types:
Categorical data (next slide) are often stored as factors – we’ll cover these in a coming lecture.
Sometimes data are non-numeric and store words. Even when that is the case, the data can be conveying different information.

R Data Types:
A survey asked respondents their name and number of cats. The instructions said to enter the number of cats as a numerical value.
How many respondents have a below average number of cats?
Giving it a first shot…
💡 maybe there is missing data in the number_of_cats column!
Oh why will you still not work??!!
Warning: There was 1 warning in `summarise()`.
ℹ In argument: `mean_cats = mean(number_of_cats, na.rm = TRUE)`.
Caused by warning in `mean.default()`:
! argument is not numeric or logical: returning NA
# A tibble: 1 × 1
mean_cats
<dbl>
1 NA
💡 What is the type of the number_of_cats variable?
cat_lovers |>
mutate(number_of_cats = case_when(name == "Ginger Clark" ~ 2,
name == "Doug Bass" ~ 3,
.default = as.numeric(number_of_cats)))Warning: There was 1 warning in `mutate()`.
ℹ In argument: `number_of_cats = case_when(...)`.
Caused by warning in `vec_case_when()`:
! NAs introduced by coercion
# A tibble: 60 × 3
name number_of_cats handedness
<chr> <dbl> <chr>
1 Bernice Warren 0 left
2 Woodrow Stone 0 left
3 Willie Bass 1 left
4 Tyrone Estrada 3 left
5 Alex Daniels 3 left
6 Jane Bates 2 left
7 Latoya Simpson 1 left
8 Darin Woods 1 left
9 Agnes Cobb 0 left
10 Tabitha Grant 0 left
# ℹ 50 more rows
⚠️ Notice the warning: as.numeric() couldn’t convert some values (the text answers) to numbers, so it made them NA. That’s R telling you your data had text in it! Here, case_when() replaces those rows, so it’s safe.
… store your data in a variable (here we’re overwriting the old cat_lovers tibble).
How many respondents have a below average number of cats?
filter() keeps only the rows that meet a condition (here, a comparison operator!). More on this in a coming lecture.
If your data does not behave how you expect it to, type coercion upon reading in the data might be the reason.
Go in and investigate your data, apply the fix, save your data, live happily ever after.
tidyverse as wellNA do in a calculation, and how do I handle it?