library(rio)
#You will have to paste your own filepath into the import command
#Notice that the ACS data is a different file type (CSV), but the import command works just fine to figure it out.
#acs <- import("C:/Users/trussler-adm/PORES Dropbox/PORES/PSCI1800/Data/ACSCountyData.csv")

#Same data in a different location on my computer
#acs <- import("C:/Users/trussler-adm/PORES Dropbox/PORES/PSCI1800/Problem Sets/Example Problem Set Folder/Data/ACSCountyData.Rdata")

#setwd("C:/Users/trussler-adm/PORES Dropbox/PORES/PSCI1800/Problem Sets/Example Problem Set Folder")
#Check to see if that worked
getwd()

#setwd("C:/Users/trussler-adm/PORES Dropbox/PORES/PSCI1800/Problem Sets/Example Problem Set Folder")
#acs <- import("Data/ACSCountyData.Rdata")

#Backup link to load acs data from the github:
acs <- import("https://github.com/marctrussler/IDS-Data/raw/main/ACSCountyData.csv")

class(acs)

#View is one of the very few capitalized commands. How did this happen? Why did they do this? What is anything?
#View(acs)

pres <- import("https://github.com/marctrussler/IDS-Data/raw/main/PresElection.Rds")
#View(pres)

names(acs)

head(acs)

tail(acs)

summary(acs)

nrow(acs)
ncol(acs)

head(acs[,9])

head(acs$population)

acs$population[2:4]

mean(acs$population)

max(acs$population)

median(acs$population)

head(acs$population/1000)

pop.1000 <- acs$population/1000

acs$pop.1000 <- acs$population/1000

ncol(acs)

acs$perc.non.white.black <- 100 - (acs$percent.white + acs$percent.black)
head(acs$perc.non.white.black)

unique(acs$census.region)

table(acs$census.region)

prop.table(table(acs$census.region))

#Uncomment and run
#table(acs$population)

head(order(acs$population))

acs[551,]

tail(order(acs$population))

acs[207,]

which(acs$median.income==max(acs$median.income,na.rm=T))
acs[2835,]

which(acs$median.income==min(acs$median.income,na.rm=T))
acs[1435,]

which(acs$median.income==median(acs$median.income,na.rm=T))
acs[1077,]


plot(acs$median.income, acs$percent.child.poverty)

acs2 <- acs
acs2$percent.child.poverty[acs2$state.abbr=="MS"] <- NA

plot(acs2$median.income, acs2$percent.child.poverty)

acs[1:10,]
acs.10 <- acs[1:10,]

#Hard Way
nrow(acs)
acs.non.100 <- acs[101:3142,]
#Easy Way
acs.non.100 <- acs[-(1:100),]

seq(2,10,2)
seq(2, nrow(acs), 2 )
acs.every.second <- acs[seq(2, nrow(acs), 2 ) ,]

table(acs$census.region)

acs.south <- acs[acs$census.region=="south",]

median(acs$population.density)

which(acs$population.density==median(acs$population.density))

acs.highdensity <- acs[acs$population.density>median(acs$population.density),]
acs.lowdensity <- acs[acs$population.density<median(acs$population.density),]

nrow(acs.highdensity) + nrow(acs.lowdensity)==nrow(acs)

head(acs$population.density<50,20)

acs$place.type <- NA

acs$place.type[acs$population.density<50]<- "rural"

acs$place.type[acs$population.density>=50 & acs$population.density<150] <-"suburban"
acs$place.type[acs$population.density>=150] <-"urban"
table(acs$place.type)

sum(table(acs$place.type))

head(acs$census.region=="south" & acs$place.type=="rural")

acs$rural.south <- NA
acs$rural.south[acs$census.region=="south" & acs$place.type=="rural"]<- 1
acs$rural.south[!(acs$census.region=="south" & acs$place.type=="rural") ]<- 0

table(!(acs$census.region=="south" & acs$place.type=="rural"),
      acs$census.region!="south" & acs$place.type!="rural")

table(!(acs$census.region=="south" & acs$place.type=="rural"),
      acs$census.region!="south" | acs$place.type!="rural")

head(acs$census.region=="south" & acs$place.type=="rural")

acs$rural.south.2 <- acs$census.region=="south" & acs$place.type=="rural"
table(acs$rural.south, acs$rural.south.2)

mean(acs$rural.south)
mean(acs$rural.south.2)

acs$percent.transit.commute[acs$state.abbr=="MO"] <- NA
table(acs$state.abbr, is.na(acs$percent.transit.commute))

acs$high.transit <- NA
acs$high.transit[acs$percent.transit.commute>median(acs$percent.transit.commute,na.rm=T)] <- 1
acs$high.transit[!acs$percent.transit.commute>median(acs$percent.transit.commute,na.rm=T)] <- 0
table(is.na(acs$high.transit))

acs$high.transit <- acs$percent.transit.commute>median(acs$percent.transit.commute,na.rm=T) 
table(is.na(acs$high.transit))

acs$high.transit <- 0
acs$high.transit[acs$percent.transit.commute>median(acs$percent.transit.commute,na.rm=T)] <- 1
table(is.na(acs$high.transit))

acs$high.transit <- NA
acs$high.transit <- acs$percent.transit.commute>median(acs$percent.transit.commute,na.rm=T) & acs$census.region=="northeast"
table(is.na(acs$high.transit))


acs.mw <- acs[acs$state.abbr=="MN" | acs$state.abbr=="WI" | acs$state.abbr=="MI" | acs$state.abbr=="OH",]
#Or: 
mw <- c("MN","WI","MI","OH")
acs.mw <- acs[acs$state.abbr %in% mw,]

acs$population[acs$population.density > median(acs$population.density)]  <- NA


acs.na.pop <- acs[is.na(acs$population),]
acs.non.na.pop <- acs[!is.na(acs$population),]


acs.sw.rural <- acs[(acs$census.region=="south" | acs$census.region=="west") & acs$place.type=="rural",]

table(acs.sw.rural$census.region)
table(acs.sw.rural$place.type)

dat <- import("https://github.com/marctrussler/IDS-Data/raw/main/BadCharacterDate.RDS")
dat

summary(dat)

nchar(dat$state)

table(nchar(dat$state))

dat$state <- gsub("_","",dat$state)
dat$state

dat$state <- gsub("[.]","",dat$state)
dat$state

dat$state <- gsub(" ","", dat$state)
dat$state

dat$state <- toupper(dat$state)
dat$state

tolower(dat$state)

#Uncomment to install
#install.packages("lubridate")

#Uncomment and run to see error
#ymd(dat$date1)

library(lubridate)

dat$date <- ymd(dat$date1)
dat$date
class(dat$date)

mean(dat$date1)
mean(dat$date)
max(dat$date)
min(dat$date)

#Assign some random values
dat$value <- runif(7,1,100)

#Plots them correctly:
plot(dat$date, dat$value)

dat$date2

dym(dat$date2)

dat$date3 <- gsub("-",".", dat$date1)
dat$date3

#install.packages("tidyr")

tidyr::separate(dat, 
                col="date3",
                into=c("year","month","day"),
                sep="[.]")

dat.new <- tidyr::separate(dat,
                col = "date3",
                into= c("year","month","day"))

#Uncomment this
#mean(dat.new$day)

class(dat.new$day)

dat.new$year <- as.numeric(dat.new$year)
dat.new$month <- as.numeric(dat.new$month)
dat.new$day <- as.numeric(dat.new$day)

mean(dat.new$day)
