UC San Diego
COGS 137 - Fall 2024
2024-10-01
Q: How much of my experience in Java, C++, and Python help me with R?
A: Tons! It’s always easier/faster to pick up a new programming language with familiarity in a prior language. Python would be the most similar to R from a design perspective, but there will still be R quirks. That said, if you’ve used `pandas` in python for data analysis, that will be very overlapping.
Q: Is attendance required to attend? I know the lectures are recorded, but I want to make sure I’m not missing out on anything if I were unable to attend for some reason.
A: You can only get the lecture attendance extra credit by being in class on any given day, but that’s just extra credit. Nothing else is required. That said, I will be forming groups within those who regularly come to class for CS01 and there will be time to work on stuff in class. So, helpful to come, but not required.
Q: Will assignments show up in DataHub?
A: They won’t, but we’ll talk about how/where to access them today/on Tues.
Q: I’m wondering how library imports work like in Python or if that’s even a thing in R!
A: It is a thing and we’ll definitely get into it! There’s a package for almost everything in R (just like in python)
Q: If you don’t submit the survey on time, will you still be assigned to the group of people that aren’t synchronously going to class?
A: 1. If the survey is still open, you can submit. 2. Yes! I will be assigning group among people who regularly come to class. If you miss a day or two, that’s ok!
Q: I’m not sure if I missed this in lecture but what is the difference between Rstudio and R, and when do we know which one to use.
A: RStudio is the environment where we write R code. We’ll be using RStudio exclusively…so RStudio will be your go to!
Q: Could the lecture survey be due at the end of the day instead?
A: I’ll think on this! It will always be open for at least an hour.
Due Dates:
Notes:
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)
So far, every variable has been an atomic vector, meaning it only stores a single piece of information.
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:
We’ll start by getting comfortable with available functions, but in a few days, you’ll learn how to write your own!
R is a dynamically typed language – it will happily convert between the various types without complaint.
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 |
Use arithmetic and comparison operators to store the value 30 in the variable var_30
and TRUE
in the variable true_var
.
install.packages
function and loaded with the library
function, once per session: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
).
“set” is in quotation marks because it is not a formal data class
A tidy data “set” can be one of the following types:
tibble
data.frame
We’ll often work with tibble
s:
readr
package (e.g. read_csv
function) loads data as a tibble
by defaulttibble
s 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.
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:
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,
TRUE ~ as.numeric(number_of_cats)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `number_of_cats = case_when(...)`.
Caused by warning:
! 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
cat_lovers |>
mutate(number_of_cats = case_when(name == "Ginger Clark" ~ 2,
name == "Doug Bass"~ 3,
TRUE ~ as.numeric(number_of_cats))) |>
summarise(mean_cats = mean(number_of_cats))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `number_of_cats = case_when(...)`.
Caused by warning:
! NAs introduced by coercion
# A tibble: 1 × 1
mean_cats
<dbl>
1 0.817
cat_lovers <- cat_lovers |>
mutate(number_of_cats = case_when(name == "Ginger Clark" ~ 2,
name == "Doug Bass"~ 3,
TRUE ~ as.numeric(number_of_cats)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `number_of_cats = case_when(...)`.
Caused by warning:
! NAs introduced by coercion
… store your data in a variable (here we’re overwriting the old cat_lovers
tibble).
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.
Before we move on…
What is the Bechdel test?
The Bechdel test asks whether a work of fiction features at least two women who talk to each other about something other than a man, and there must be two women named characters.
Concepts introduced:
See this week’s lab!
Try to play around with this after finishing your lab!
tidyverse
as well