pres <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/CountyPresData2020.Rds")

states <- unique(pres$state)
biden.votes <- rep(NA, length(states))
trump.votes <- rep(NA, length(states))

for(i in 1:length(states)){
  biden.votes[i] <- sum(pres$biden.votes[pres$state==states[i]])
  trump.votes[i] <- sum(pres$trump.votes[pres$state==states[i]])
}

head(cbind(states,biden.votes,trump.votes))

# states <- unique(pres$state)
# biden.votes <- rep(NA, length(states))
# trump.votes <- rep(NA, length(states))
# winners.margin <- rep(NA, length(states))
# for(i in 1:length(states)){
#   biden.votes[i] <- sum(pres$biden.votes[pres$state==states[i]])
#   trump.votes[i] <- sum(pres$trump.votes[pres$state==states[i]])
# 
#   #if biden votes is greater than trump votes
#   winners.margin[i] <- biden.votes[i] - trump.votes[i]
#   #Otherwise
#   winners.margin[i] <- trump.votes[i] - biden.votes[i]
# }
# 
# head(cbind(states,biden.votes,trump.votes))

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 2")
}
print("I ran code chunk 3")

if(2+2==4){
  print("I ran code chunk 2")
}
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")
}

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

states <- unique(pres$state)
biden.votes <- rep(NA, length(states))
trump.votes <- rep(NA, length(states))
winners.margin <- rep(NA, length(states))
for(i in 1:length(states)){
  biden.votes[i] <- sum(pres$biden.votes[pres$state==states[i]])
  trump.votes[i] <- sum(pres$trump.votes[pres$state==states[i]])
  
  if(biden.votes[i] >trump.votes[i]){
    winners.margin[i] <- biden.votes[i] - trump.votes[i]
  } else {
    winners.margin[i] <- trump.votes[i] - biden.votes[i]
  }
}
cbind(states, winners.margin)

plot(1:length(states), winners.margin)

states <- unique(pres$state)
biden.votes <- rep(NA, length(states))
trump.votes <- rep(NA, length(states))
winners.margin <- rep(NA, length(states))
for(i in 1:length(states)){
  biden.votes[i] <- sum(pres$biden.votes[pres$state==states[i]])
  trump.votes[i] <- sum(pres$trump.votes[pres$state==states[i]])
  
  if(biden.votes[i] >trump.votes[i]){
    winners.margin[i] <- biden.votes[i] - trump.votes[i]
  } 
  
  winners.margin[i] <- trump.votes[i] - biden.votes[i]

}

plot(1:length(states), winners.margin)

set.seed(19104)
stay <- NA
switch <- NA
#Stage one of game, randomly select a door
selection <- sample(c("goat","goat","car"),1)
selection
#We selected a goat, Monty knows this, but we don't. 
#If we stay we get what we selected
stay <- selection
#The outcome of switching depends on our selection.
#If we selected a goat initially, monty reveals the other goat
#and swithing gets us a car. 
#If we selected a car initially, monty reveals one of the two goats
#and swithing gets us a goat
if(selection=="goat"){
switch <- "car"
} else if (selection=="car"){
switch <- "goat"
}
stay
switch

stay <- NA
switch <- NA

for(i in 1:10000){
  #Stage one of game, randomly select a door
  selection <- sample(c("goat","goat","car"),1)
  selection
  #We selected a goat, Monty knows this, but we don't. 
  #If we stay we get what we selected
  stay[i] <- selection
  #The outcome of switching depends on our selection.
  #If we selected a goat initially, monty reveals the other goat
  #and swithing gets us a car. 
  #If we selected a car initially, monty reveals one of the two goats
  #and swithing gets us a goat
  if(selection=="goat"){
    switch[i] <- "car"
  } else if (selection=="car"){
    switch[i] <- "goat"
  }
}
mean(stay=="car")
mean(switch=="car")


x <- 1:10
sum(x)/length(x)

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)

pres$total.votes <- pres$biden.votes + pres$trump.votes + pres$other.votes

#To create a vote share, in percent:

vs <- round(100*(pres$biden.votes/pres$total.votes),2)
head(vs)

#Put that into a function

vote.share <- function(cand.votes, total.votes){
round(100*(cand.votes/total.votes),2)
}


vs <- vote.share(cand.votes=pres$biden.votes, total.votes = pres$total.votes)
head(vs)

#R will assume first you put is first argument, second thing is second argument etc. 
vs <- vote.share(pres$trump.votes, pres$total.votes)
head(vs)

votes <- pres[,4:7]
head(votes)

#Preserve original data by creating a new dataset
tmp <- votes
#Loop across the columns. For each column divide by that column's standard deviation
for(i in 1:ncol(tmp)){
  tmp[,i] <- tmp[,i]/sd(tmp[,i])
}
head(tmp)
#Standardized variables have a sd of 1 by definition:
sd(tmp$biden.votes)

#Put this in a function
stdrdz <- function(dat){
  for(i in 1:ncol(dat)){
    dat[,i] <- dat[,i]/sd(dat[,i])
  }
}

tmp <- stdrdz(votes)
head(tmp)
#Nothing here??


stdrdz <- function(dat){
  for(i in 1:ncol(dat)){
    dat[,i] <- dat[,i]/sd(dat[,i])
  }
  #Explicitly say: return dat as the output of this function
  return(dat)  
}

tmp <- stdrdz(votes)
head(tmp)

stdrdz <- function(dat, mean.centered){
  if(mean.centered==T){
    for(i in 1:ncol(dat)){
      dat[,i] <- (dat[,i]-mean(dat[,i]))/sd(dat[,i])
    }
  }else { 
    for(i in 1:ncol(dat)){
      dat[,i] <- dat[,i]/sd(dat[,i])
    }
  }
  #Explicitly say: return dat as the output of this function
  return(dat)  
}

tmp <- stdrdz(votes, mean.centered = T)
head(tmp)
mean(tmp$biden.votes)

tmp <- stdrdz(votes, mean.centered = F)
head(tmp)
mean(tmp$biden.votes)

stdrdz <- function(dat, mean.centered=T){
  if(mean.centered==T){
    for(i in 1:ncol(dat)){
      dat[,i] <- (dat[,i]-mean(dat[,i]))/sd(dat[,i])
    }
  }else { 
    for(i in 1:ncol(dat)){
      dat[,i] <- dat[,i]/sd(dat[,i])
    }
  }
  #Explicitly say: return dat as the output of this function
  return(dat)  
}

#By default mean centers: 
tmp <- stdrdz(votes)
mean(tmp$biden.votes)

#Can override:
tmp <- stdrdz(votes, mean.centered=F)
mean(tmp$biden.votes)

#The things we want to generate are state, winner, dem perc, rep perc, and total votes
input.state <- "AL"

#Define state for output
state <- input.state

#Calculate the statewide dem and rep perc
dem.perc <- round(100*(sum(pres$biden.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)
rep.perc <- round(100*(sum(pres$trump.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)

#Use and if/else statement to define the winner based on the relative size of dem and rep percent, which we just defined
#No states are ties
if(dem.perc>rep.perc){
  winner = "Biden"
} else { 
  winner = "Trump"
  }

#Define total votes
total.votes <- sum(pres$total.votes[pres$state==input.state])

#Make output dataframe
out <- cbind.data.frame(state, winner, dem.perc, rep.perc, total.votes)

names(out) <- c("State", "Winner", "Democratic Percent", "Republican Percent", "Total Votes")

#Need kableExtra package for this part to work:
kableExtra::kable(out)

#The things we want to generate are state, winner, dem perc, rep perc, and total votes
input.state <- "PA"

#Define state for output
state <- input.state

#Calculate the statewide dem and rep perc
dem.perc <- round(100*(sum(pres$biden.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)
rep.perc <- round(100*(sum(pres$trump.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)

#Use and if/else statement to define the winner based on the relative size of dem and rep percent, which we just defined
#No states are ties
if(dem.perc>rep.perc){
  winner = "Biden"
} else { 
  winner = "Trump"
  }

#Define total votes
total.votes <- sum(pres$total.votes[pres$state==input.state])

#Make output dataframe
out <- cbind.data.frame(state, winner, dem.perc, rep.perc, total.votes)

names(out) <- c("State", "Winner", "Democratic Percent", "Republican Percent", "Total Votes")

#Need kableExtra package for this part to work:
kableExtra::kable(out)


state.table <- function(input.state){
  #Define state for output
  state <- input.state
  
  #Calculate the statewide dem and rep perc
  dem.perc <- round(100*(sum(pres$biden.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)
  rep.perc <- round(100*(sum(pres$trump.votes[pres$state==input.state])/sum(pres$total.votes[pres$state==input.state])),1)
  
  #Use and if/else statement to define the winner based on the relative size of dem and rep percent, which we just defined
  #No states are ties
  if(dem.perc>rep.perc){
    winner = "Biden"
  } else { 
    winner = "Trump"
  }
  
  #Define total votes
  total.votes <- sum(pres$total.votes[pres$state==input.state])
  
  #Make output dataframe
  out <- cbind.data.frame(state, winner, dem.perc, rep.perc, total.votes)
  
  names(out) <- c("State", "Winner", "Democratic Percent", "Republican Percent", "Total Votes")
  #Output is that table
  return(out)
}


kableExtra::kable(state.table("TX"))

kableExtra::kable(state.table("WI"))

#First remove the function so we can show source() works
rm(state.table)

#Read the file from github. 
source("https://github.com/marctrussler/IDS-Data/raw/refs/heads/main/StateTable.R")

#Use the function
kableExtra::kable(state.table("MN"))

