2 == 1+1

1/2 == .5

2 == 3

2/3 == .6666

vec <- c(1,2,3,4,5)

vec == 3

2 > 3

2 < 3

vec > 3

vec >= 3

vec <= 3

sum(vec >= 3)

mean(vec >= 3)

2 == 3 #FALSE

2 != 3 #TRUE

!(2 == 3) #TRUE. essentially the same as the one above

vec == 3
!(vec == 3)

(2 == 2) & (3 == 3) #TRUE

(2 == 2) & (3 == 2) #FALSE

(2 == 2) | (3 == 2) #TRUE

(2 == 8) | (3 == 2) #FALSE

buffalo.springfield <- c("stills", "martin","palmer","furay","young")
csn <- c("crosby","stills","nash")
csny <- c("crosby","stills","nash","young")

buffalo.springfield %in% csn
buffalo.springfield %in% csny

acs <- rio::import("https://github.com/marctrussler/IIS-Data/raw/main/ACSCountyData.csv")
head(acs[,c("county.name","state.abbr","median.income","percent.college")])

head(acs$median.income[acs$percent.college > 40])

head(acs[acs$state.abbr == "PA", ])

#Counties in Pennsylvania with over 40% college educated:
head(acs[acs$state.abbr == "PA" & acs$percent.college > 40, ])

northeast <- c("PA","NY","NJ","CT","MA","VT","NH","ME","RI")
head(acs[acs$state.abbr %in% northeast, ])

mean(acs$population)

mean(acs$population[acs$state.abbr=="AL"])

mean(acs$population[acs$state.abbr=="AK"])
mean(acs$population[acs$state.abbr=="AZ"])
mean(acs$population[acs$state.abbr=="AR"])
mean(acs$population[acs$state.abbr=="CA"])
#
#
#
mean(acs$population[acs$state.abbr=="WI"])
mean(acs$population[acs$state.abbr=="WY"])

for(i in 1:3){
  #CODE HERE
}

for(i in 1:3){
  print(i)
}

print(1)
print(2)
print(3)

for(batman in 1:3){
  print(batman)
}

states <- unique(acs$state.abbr)
head(states)
length(states)

states[1]
states[13]
states[27]

for(i in 1:3){
  mean(acs$population[acs$state.abbr==states[i]])
}

#What it will literally do
mean(acs$population[acs$state.abbr==states[1]])
mean(acs$population[acs$state.abbr==states[2]])
mean(acs$population[acs$state.abbr==states[3]])

for(i in 1:length(states)){
  mean(acs$population[acs$state.abbr==states[i]])
}

state.pop.means <- rep(NA, length(states))

for(i in 1:length(states)){
  state.pop.means <- mean(acs$population[acs$state.abbr==states[i]])
}
state.pop.means

state.pop.means <- rep(NA, length(states))

for(i in 1:length(states)){
  state.pop.means[i] <- mean(acs$population[acs$state.abbr==states[i]])
}

head(cbind(states, state.pop.means))

coin <- c("H","T")

sample(coin, 1)

sample(coin, 1)
sample(coin, 1)
sample(coin, 1)
sample(coin, 1)
sample(coin, 1)

set.seed(19104)
sample(coin, 1)

for(i in 1:1000){
  sample(coin, 1)
}

result <- rep(NA, 1000)
for(i in 1:1000){
 result[i] <- sample(coin, 1)
}
head(result)

result <- rep(NA, 1000)
for(i in 1:1000){
 result[i] <- sample(coin, 1) == "H"
}
head(result)

mean(result)

dice <- c(1,2,3,4,5,6)

#Using the replace=T option because each time we sample from the dice
#we want all sides to be available
sum(sample(dice, 3, replace=T))
#Same with the coin
sample(coin, 2, replace=T)

#This one is easy: is the sum greater than 12
sum(sample(dice, 3, replace=T))>=12

#For the coin we want to see if each entry is equal to "H", but only return one true
#if both of them are H. We can do that with all()
sample(coin,2, replace=T)=="H"

all(sample(coin,2, replace=T)=="H")

#And then we want to know if both conditions are met:

sum(sample(dice, 3, replace=T))>=12 & all(sample(coin,2, replace=T)=="H")

result <- rep(NA, 1000)
for(i in 1:1000){
  result[i] <- sum(sample(dice, 3, replace=T))>=12 & all(sample(coin,2, replace=T)=="H")
}

mean(result)

coin <- c(0,1)
samp <- sample(coin, 1000, replace=T, prob=c(.7,.3))
#Lower bound less than .3 and upper bound greater than .3?
t.test(samp)$conf.int[1] < .3 & t.test(samp)$conf.int[2] > .3

result <- rep(NA, 1000)
for(i in 1:1000){
  samp <- sample(coin, 1000, replace=T, prob=c(.7,.3))
  #Lower bound less than .3 and upper bound greater than .3?
  result[i] <- t.test(samp)$conf.int[1] < .3 & t.test(samp)$conf.int[2] > .3
}
mean(result)

anes <- read.csv("https://raw.githubusercontent.com/marctrussler/IIS-Data/main/ANES2020Clean.csv")

table(anes$race)

prop.table(table(anes$race))

table(race = anes$race, gender = anes$gender)

prop.table(table(race = anes$race, gender = anes$gender))

prop.table(table(race = anes$race, gender = anes$gender), 1)

prop.table(table(race = anes$race, gender = anes$gender), 2)

say.hello <- function(){
  print("Hello!")
}

say.hello()

say.hello

new.mean <- function(vec){
  sum(vec)/length(vec)
}

x <- 1:175
new.mean(vec=x)

new.mean(x)
#To show it works with other things
y <- 4:17
new.mean(y)

random.combo <- function(dial, entries){
  sample(1:dial, entries, replace=F)
}

set.seed(19104)
#My Pottruck locker
random.combo(dial=40, entries=3)

#Some fancy safe
random.combo(dial=100, entries=5)

#The tiniest lock in the world
random.combo(dial=10, entries=2)

set.seed(19104)
truth <- random.combo(dial=40, entries=3)
truth

guess <- random.combo(dial=40, entries=3)
guess == truth

all(guess == truth)

set.seed(19104)
truth <- random.combo(dial=40, entries=3)

result <- rep(NA, 100000)
for(i in 1:100000){
  guess <- random.combo(dial=40, entries=3)
  result[i] <- all(guess == truth)
}
mean(result)

if(2+2==4){
  print("I ran code chunk 1")
}

if(2+2==5){
  print("I ran code chunk 2")
}

if(2+2==5){
  print("I ran code chunk 2")
} else {
  print("I ran code chunk 3")
}

if(2+2==5){
  print("I ran code chunk 1")
} else if(2+2==4){
  print("I ran code chunk 2")
} else {
  print("I ran code chunk 3")
}
