library(tidyr)

#THe first chunks of code here are for demonstration purposes for the textbook
dat <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/WideExample.RDS")
dat

dat.l <- pivot_longer(dat,
                      cols= date_1_1_2021:date_1_4_2021,
                      names_to="date",
                      values_to =" covid.cases")
dat.l

dat2 <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/LongExample.RDS")
dat2

dat2.w <- pivot_wider(dat2,
                      id_cols = district,
                      names_from = candidate, 
                      values_from = votes)
dat2.w

dat2.w <- pivot_wider(dat2, 
                      id_cols = candidate,
                      names_from = district, 
                      values_from = votes)
dat2.w

#Code to be learned starts here
library(tidyr)
dat <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/WideExample.RDS")
dat

pivot_longer(dat, 
             cols = "date_1_1_2021":"date_1_4_2021",
             names_to = "date", 
             values_to = "covid.cases")

pivot_longer(dat, 
             cols = "date_1_1_2021":"date_1_4_2021",
             names_to = "date", 
             values_to = "covid.cases")

pivot_longer(dat, 
             cols = "date_1_1_2021":"date_1_4_2021",
             names_to = "batman", 
             values_to = "robin")


dat.l <- pivot_longer(dat, 
             cols = "date_1_1_2021":"date_1_4_2021",
             names_to = "date", 
             values_to = "covid.cases")
dat.l

dat.w <- pivot_wider(dat.l,
                names_from = "date",
                values_from = "covid.cases")
dat.w

# pivot_wider(dat.l,
#                names_from = "month",
#                values_from = "cases")


pivot_wider(dat.l,
                names_from = "county",
                values_from = "covid.cases")

pivot_wider(dat.l,
                names_from = "county",
                values_from = "covid.cases")

dat2

dat2.w <- pivot_wider(dat2,
                      names_from ="candidate", 
                      values_from = "votes")
dat2.w

dat2.w$total.votes <- dat2.w$Biden + dat2.w$Trump
dat2.w$perc.biden <- dat2.w$Biden/dat2.w$total.votes
dat2.w$perc.trump <- dat2.w$Trump/dat2.w$total.votes

dat2.w

dat2.table <- dat2.w
dat2.table$Biden <- NULL
dat2.table$Trump <- NULL
dat2.table$total.votes <- NULL
dat2.table

to.keep <- c("district","perc.biden","perc.trump")
dat2.table <- dat2.w[to.keep]
dat2.w <- dat2.w[to.keep]

dat2.table

round(717.128210, 0)

dat2.table$perc.biden <- round(dat2.table$perc.biden*100,2)
dat2.table$perc.trump <- round(dat2.table$perc.trump*100,2)

dat2.table

dat.2.candidates <- pivot_wider(dat2,
                      names_from = "district", 
                      values_from = "votes")
dat.2.candidates

library(tidyr)
library(lubridate)

genfor <- rio::import("https://github.com/marctrussler/IDS-Data/raw/main/Genfor.RDS")

head(genfor)

genfor.untouched <- genfor

nrow(genfor)
length(unique(genfor$GENF_ID))

head(genfor[c("GENF_ID","approval.party","approval.value")])

genfor[1,] == genfor[2,] 

genfor <- pivot_wider(genfor, 
                      names_from = "approval.party",
                      values_from = "approval.value")

head(genfor)

attributes(genfor.untouched$Q10A_1)
attributes(genfor.untouched$Q10A_2)
attributes(genfor.untouched$Q10A_16)


genfor <- pivot_longer(genfor, 
                       cols = "Q10A_1":"Q10A_22",
                       names_to = "top.issue", 
                       values_to = "val")

nrow(genfor)
#View(genfor)

genfor <- genfor[genfor$val != 0,]
nrow(genfor)

#View(genfor)

head(duplicated(genfor$GENF_ID))

genfor <- genfor[!duplicated(genfor$GENF_ID),]
head(genfor)

genfor$top.issue[genfor$val == 99] <- NA
genfor$val <- NULL 

names(genfor)

names(genfor) <- tolower(names(genfor))

names(genfor)
attributes(genfor.untouched$Q0) # this one is 2016 presidential vote
attributes(genfor.untouched$Q1) # this one is Trump approval

names(genfor)[2] <- "weight"
names(genfor)[3] <- "vote2016"
names(genfor)[4] <- "approve.trump"
names(genfor)[13] <- "approve.dem"
names(genfor)[14] <- "approve.rep"

names(genfor) <- gsub("_",".",names(genfor))

summary(genfor)

class(genfor$weight)

is.numeric(genfor$weight) 

is.factor(genfor$weight) 

head(as.numeric(genfor$weight)) # wrong

head(as.numeric(as.character(genfor$weight)))

summary(as.numeric(genfor$weight))
summary(as.numeric(as.character(genfor$weight))) 

genfor$weight <- as.numeric(as.character(genfor$weight))

head(genfor$date) 
class(genfor$date)

genfor$date <- ymd(genfor$date) 
class(genfor$date)

table(genfor$date) 

#View(genfor[genfor$date >= "2017-11-01",])

table(genfor$vote2016)
attributes(genfor.untouched$Q0)

genfor$vote2016[genfor$vote2016 %in% c(98,99)] <- NA
summary(genfor$vote2016)
table(genfor$vote2016)

genfor$vote2016[genfor$vote2016==1] <- "Clinton"
genfor$vote2016[genfor$vote2016==2] <- "Trump"
genfor$vote2016[genfor$vote2016==3] <- "Other"
genfor$vote2016[genfor$vote2016 %in% c(4,98,99)] <- NA
table(genfor$vote2016)
head(genfor$vote2016)

attributes(genfor.untouched$gender)

genfor$gender[genfor$gender == 1] <- "M"
genfor$gender[genfor$gender == 2] <- "F"
genfor$gender[genfor$gender == 0] <- "U"

genfor$female <- NA
genfor$female[genfor$gender=="M" | genfor$gender=="U"] <- 0
genfor$female[genfor$gender=="F"] <- 1

genfor2 <- separate(genfor,
         col = "date",
         into = c("year","month","day"))
head(genfor2)

genfor <- separate(genfor,
                   col = "date",
                   into = c("year","month","day"),
                   sep = "-")

genfor$trump <- NA
genfor$trump[genfor$vote2016=="Trump"]<- 1
genfor$trump[genfor$vote2016!="Trump"]<- 0

table(genfor$trump)

plot(genfor$age,genfor$trump)

boxplot(genfor$age ~ genfor$trump,
        xlab="Vote for Trump",
        ylab="Age",
        main="Distribution of Age for Trump and Non-Trump Voters")

cor(genfor$age, genfor$trump)

cor(genfor$age, genfor$trump, use="pairwise.complete")

plot(density(genfor$age[genfor$trump==1],na.rm=T),
     xlab="Age",
     main="Distribution of Age for Trump and Non-Trump Voters",
     col="firebrick",
     ylim=c(0,0.1))
points(density(genfor$age[genfor$trump==0],na.rm=T), type="l",
       col="dodgerblue")
legend("topright",c("Trump Voters","Non-Trump Voters"),
       lty=c(1,1), col=c("firebrick","dodgerblue"))
