poli380 - lecture 1-5

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/118

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:36 AM on 9/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

119 Terms

1
New cards

layout of r studio

r script = top left

environment = top right

r console = bottom left

help and plots = bottom right

2
New cards

r script

write and run code

3
New cards

r console

where r puts executed code and outputs (as well as errors)

4
New cards

what colour is executed code

blue

5
New cards

what colour is output of executed code

black

6
New cards

[1] thingy meaning

means the output to its right is the first output in this case

7
New cards

r script ___ code

saves

8
New cards

<

less than

9
New cards

<=

less than or equal to

10
New cards

!=

not equal to

11
New cards

==

must use both of these for conditionals!!!!!!

like “is this true or nah”

12
New cards

and symbol

&

13
New cards

or symbol

|

14
New cards

what’s the caveat for the or symbol

it won’t tell you which one is true

15
New cards

what does creating objects do

it’s how r stores info

like a box that contains something

16
New cards

object operator

←

17
New cards

sequence for creating an object

object_name ← object_content

18
New cards

how do i find the content of the object

object_name in R!!!!!

it’s basically saying what is inside this

19
New cards

where will the content of the object show up

environment!!!!!

20
New cards

rules for naming objects

cannot begin with a number/have spaces/special symbols

for textual content, use quotes

21
New cards

why do we use quotes for text

if you don’t it’ll assume the text is an object. and it' won’t work b/c the object hasn’t been named

22
New cards

when do we use quotes when writing code

not for names of objects, functions, arguments or special values

u use it for all other texts unless this choice is intentional

23
New cards

what’s this weird thing that r will do (hint: override)

it’ll override objects if you assign new content to existing object name

also r is case sensitive

24
New cards

what are vectors

objects with a series of values

“multiple elements of the same type”

25
New cards

what function do vectors use

c() COMBINE BABY

26
New cards

what are functions

actions you require r to perform on particular object or data

takes inputs → action → output

functions can also be applied to vectors

27
New cards

what if you have a combined function with “na”

no value so if u wanna calculate mean or smth u gotta deal w that

28
New cards

mean/median when you have na

mean(y, na.rm = TRUE)

median(y, na.rm = TRUE)

29
New cards

what is a name of a function followed by

function_name

PARANTHESIS

30
New cards

how many required arguments are needed (at least)

1, for most functions

31
New cards

optional arguments

are optional. not rlly necessary unless needed

32
New cards

what do you do if you have multiple arguments

function_name(argument 1, argument 2)

33
New cards

how do you specify arguments

put priority or include number of arguments in in specifics

if more than 1 req arg, put em in that order

34
New cards

okay what are the two formats we r using

function_name (required_argument, optional_argumentname = optional_argument)


function_name(required_argument)

35
New cards

packages

extend the functionality of r

36
New cards

what do we do with packages

install them + load

(installing is only done once tbh, load everytime)

37
New cards

sequence for installing packages

install.packages (“causaldata”)

38
New cards

loading packages

library(causaldata)

39
New cards

#

commenting code; r ignores everything after # until end of line

40
New cards

what are datasets/data

they capture characteristics of a particular set of individuals or entities

41
New cards

how are datasets organzied

as dataframes

42
New cards

rows =

observations

43
New cards

columns

variables

44
New cards

what are observations

information collected from a particular entity or individual in the study

45
New cards

unit of observation

defines the individual or entity that each observation in a dataframe represents; each observation is a row number, denoted as i

46
New cards

what are variables

values of changing characteristics for various individuals and entities in the study

47
New cards

how do we refer to variables

by its name

48
New cards

notation for defining a new variable

x = {10, 5, 8}


where x is the name of the variable

and the numbers are the content of the variables; multiple observations

49
New cards

each individual observation is

iiiiiiii


the observation number

50
New cards

what is data wrangling

allows us to make data in the form we want

51
New cards

tibble

how tidyverse stores dataframes

52
New cards

what are the three methods for datasets within r

some are built in, others are acquired via loading r packages, and others — we read em in ourselves

53
New cards

names()

gives us the names of columns

54
New cards

head()

first 6 rows and observations in a dataset

55
New cards

dim()

dimensions of a dataset

number of rows and columns

56
New cards

n(row)

number of rows

57
New cards

n(col)

number of columns

58
New cards

View() (w a capital)

contents of the entire dataset

59
New cards

what tf is dplyr

gives us a consistent set of verbs for data manipulation

60
New cards

how to get that dplyr

install.packages (“dplyr”)

library(dplyr)

61
New cards

pipe

used to write code sequentially %>%

62
New cards

base r vs the pipe

base r sqrt(sum(range(student_ages)))


pipe

student_ages %>%

range()%>%

sum()%>%

sqrt()

63
New cards

filter()

selects rows that satisfy the argument we provide it

64
New cards

how do we save as a new dataset

we must create a new object

65
New cards

subsets in base r

flights_AA2 ← flights [flights $ carrier == “AA”]

66
New cards

filtering on multiple conditions

flights%>%

filter(dest “LAS” | dest “LAX)


(w two equal signs)

notice how you put dest TWICE

67
New cards

new dataframe

new object

68
New cards

what does - do


removes columns


flights%>%

select(-carrier, -dep_delay)

69
New cards

you can combine dplyr functions

by pipe’in, filter, select

70
New cards

rename

(new_name = old_name)

71
New cards

what does mutate do

operates on the existing column

adds new row

(new_var = operation on existing)

72
New cards

in base r….

flightsdistancekm.←flightsdistance_km. ← flightsdistance * 1.6

73
New cards

summarize()

calculates the summaries of variables


like mean, max, min


it can also count the number of rows in a dataframe

74
New cards

group_by()

applies a function to a group of observations; divides data into groups based on variables

75
New cards

what’s the note about groupby function

it doesn’t change the data. subsequent functions will make that mess

76
New cards

what happens after you add summarize() after groupby function

you get a stack of descriptive statistics which is awesome


example


flights %>%

group_by (month) %>%

summarize(n_obs = n()))

77
New cards

steps for working directory

set wd to 380data

load dataset using read.csv

understand the data by using View and head

identify the number of observations and variables

78
New cards

read.csv()

reads csv files


the required argument is the name of the csv file in quotes

79
New cards

types of variables

character and numeric

80
New cards

type of character variables

categorical (2+ text categories)

81
New cards

types of numeric variables

binary - only two

non binary - more than two

82
New cards

binary

only two values, 1s and 0s

presence or absence of a trait

83
New cards

non binary

more than 2 values

84
New cards

character variables

contain text

often categorical; take on a limited number of values

85
New cards

binary numeric variables

numeric encodings tha could be coded as character variables with two categories

(1, 0) or (voted, didn’t vote)

86
New cards

avg/mean of a variable

sum of all values across all observations divided by total number of observations

87
New cards

x bar

avg of x

88
New cards

that weird e to the power of n and subscript 1 = 1 x i

sum of all xi (observations of x) from i = 1 to i = n, 1st observation of x to the last

89
New cards

xi

observation x where i = position of observation and n = total number of observations in the variable

90
New cards

non binary mean of a variable

as an average; same units as the variable (1 or 0)

91
New cards

binary mean of a variable

as a proportion in %; after multiplying by 100

92
New cards

why are binary means a proportion

bc mean of a binary variable = proportion to observations that have that trait

93
New cards

for categorical variables with more than 2 categories

there’s no straightforward interpretation

94
New cards

numeric/double variables

decimals

95
New cards

integer variables

whole numbers

96
New cards

factor variables

categorical

97
New cards

ggplot2 package (what does it do)

basically just the grammar of graphics framework

98
New cards

ggplot2

a statistical graphic; mapping of data variables to aesthetic attributes of gramatical objects

99
New cards

data

datasets w/ variables

100
New cards

geom

geometric objects we wish to plot (points + bars)