library(tidyr)

anes <- rio::import("https://github.com/marctrussler/IIS-Data/raw/main/ANES2020Clean.csv")

boxplot(anes$income ~ anes$ideology7, xlab="Ideology",
        ylab="Reported Family Income (Thousands)", outline=F, axes=F)
axis(side=2)
axis(side=1, at=1:7, 
     labels=c("Very Liberal","2","3","4","5","6","Very Conservative"), cex.axis=0.8)

set.seed(2105)
county <- c("a","b","c","d")
x <- round(runif(4,min=1,max=10))
y <- round(runif(4,min=1,max=10))

dat1 <- cbind.data.frame(county,x)
dat2 <- cbind.data.frame(county,y)

kableExtra::kable(dat1)
kableExtra::kable(dat2)

kableExtra::kable(cbind.data.frame(dat1,y))

dat2 <- cbind.data.frame(rev(county),rev(y))

kableExtra::kable(dat2)

set.seed(2105)
county <- c("a","a.lag","b","b.lag","c","d", "e")
y <- round(runif(7,min=1,max=10))

dat2 <- cbind.data.frame(county,y)
kableExtra::kable(dat2)

ID <- c(1,2,3,4,5)
county <- c("Wakeusha WI", "Erie PA", "Delaware OH", "Broward FL", "Wakeusha WI")
ideology <- c(3,5,2,6,1)
dem.perc <- rep("?",5)

dat <- cbind.data.frame(ID, county,ideology, dem.perc)
kableExtra::kable(dat)

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

head(x)

x$candidate <- NULL
elect <- pivot_wider(x,
                names_from="party",
                values_from="candidatevotes")
keep <- c("year","state","county","FIPS","totalvotes")
elect <- elect[keep]
elect.all <- elect
elect <- elect[elect$year==2016,]
elect <- elect[!is.na(elect$FIPS),]
#Removing this dataset x, only because it makes things confusing below, usually
#i'd keep it in the environment.
rm(x)

head(elect)

acs <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/R12662248_SL051.csv")

head(acs)

acs$vap <- acs$SE_A01001B_005

acs$Geo_NAME[acs$Geo_FIPS==27137]
elect$county[elect$FIPS==27137]

keep <- c("Geo_FIPS","vap")
acs <- acs[keep]

head(acs)
head(elect)

class(elect$FIPS)
class(acs$Geo_FIPS)

table(nchar(acs$Geo_FIPS))
table(nchar(elect$FIPS))

acs2 <- acs
#Use the paste command to add leading 0 to those fips codes with 
#only 4 characters

acs2$Geo_FIPS[nchar(acs2$Geo_FIPS)==4] <- paste("0",acs2$Geo_FIPS[nchar(acs2$Geo_FIPS)==4], sep="")
table(nchar(acs2$Geo_FIPS))
head(acs2$Geo_FIPS)

table(elect$FIPS %in% acs$Geo_FIPS)

unmatched <- elect[!(elect$FIPS %in% acs$Geo_FIPS),]
head(unmatched,41)

merged.data <- merge(elect, acs, by.x="FIPS", by.y="Geo_FIPS")
head(merged.data)

sum(is.na(merged.data$vap))

merged.data <- merge(elect, acs, by.x="FIPS", by.y="Geo_FIPS", all.x=T)
sum(is.na(merged.data$vap))

merged.data <- merged.data[!merged.data$state=="Alaska",]

merged.data$perc.turnout <- (merged.data$totalvotes/merged.data$vap)*100

mean(merged.data$perc.turnout,na.rm=T)

sum(merged.data$totalvotes,na.rm=T)/sum(merged.data$vap,na.rm=T)


boxplot(merged.data$perc.turnout ~ merged.data$state, outline=F)

head(elect.all[order(elect.all$FIPS),])

full.merge <- merge(elect.all, acs, by.x="FIPS",by.y="Geo_FIPS",all.x=T)
head(full.merge[order(full.merge$FIPS),])

#This is made up data
acs.series <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/ACSSeries.Rds")
acs.series <- acs.series[order(acs.series$Geo_FIPS),]
head(acs.series,10)

elect.all <- elect.all[order(elect.all$FIPS),]
head(elect.all,10)

#Bad, many-to-many merge. Don't do this:
bad <- merge(elect.all, acs.series, by.x="FIPS",by.y="Geo_FIPS")

acs.series$county.year <- paste(acs.series$Geo_FIPS,acs.series$year,sep="")
head(acs.series)
elect.all$county.year <- paste(elect.all$FIPS,elect.all$year,sep="")
head(elect.all)
#If the variable is called the same thing in both datasets you don't have to do by.x and by.y:
merge.series <- merge(elect.all,acs.series, by="county.year", all.x=T)
head(merge.series)

#Going to delete the merging variable we made first:
elect.all$county.year <- NULL
acs.series$county.year <- NULL

merge.series2 <- merge(elect.all, acs.series, by.x=c("FIPS","year"), by.y=c("Geo_FIPS","year"))
head(merge.series2)
