2 == 1+1
2 == 2+2

2/3== .666

2>3
2<3

2<=2
2>=3

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

vec >= 3

vec >= 3
sum(vec >= 3)

mean(vec >= 3)

2!=3

vec==3
!(vec==3)

vec <- 1:10
3 %in% vec 

3==vec

vec1 <- 1:30
vec2 <- 25:50

vec1 %in% vec2


vec2 %in% vec1

library(rio)
population.table <- import("https://github.com/marctrussler/IDS-Data/raw/main/populationtable.Rds")

emissions <- population.table[,4]
emissions

emissions[4:7]

emissions[c(F,F,F,T,T,T,T)]

emissions<20

emissions[emissions<20]

world.pop <- population.table[,2]
world.pop[world.pop>4000]

population.table[emissions<20,]

# population.table[population.table[,2]>4000,]

#Uncomment and try to run:
#population.table[c(T,T,F,F,F,F,F,T,T,F,F),]

population.table[c(T,F,F),]

population.table[population.table[,4]> median(population.table[,4]),]

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

buffalo.springfield[buffalo.springfield %in% csny]

buffalo.springfield[!(buffalo.springfield %in% csny)]

buffalo.springfield[csny %in% buffalo.springfield]

#Both statements are T
(2 == 2) & (3 == 3) 

#One of the statements is T, the other F
(2 == 2) & (3 == 2)

#Both statements are F
(2 == 4) & (3 == 2)

#All are T
(2 == 2) & (3 == 3) & (2>1) & ("yes" %in% c("yes","no"))

#Two are false
(2 == 2) & (3 == 3) & (2<1) & ("maybe" %in% c("yes","no"))

#Both statements are T
(2 == 2) | (3 == 3) 

#One of the statements is T, the other F
(2 == 2) | (3 == 2)

#Both statements are F
(2 == 4) | (3 == 2)

population.table[population.table[,2] > 4000 & population.table[,4] >20,]

population.table[population.table[,2] > 4000 | population.table[,4] >20,]

music <- c("meh","good","meh","bad","good","bad","good")

population.table.new <- cbind(population.table, music)

population.table.new

mean(population.table.new[,2])

population.table <- cbind.data.frame(population.table,music)
population.table

population.table[population.table[,5]=="good" | population.table[,5]=="meh",]

population.table[population.table[,5] %in% c("good","meh"),]

population.table[!(population.table[,5]=="good" | population.table[,5]=="meh"),]

population.table[population.table[,5]=="bad",]

population.table[population.table[,5]=="good" & population.table[,5]=="meh",]

population.table[population.table[,2]>3000
                 & population.table[,5]=="meh"
                 | population.table[,5]=="good",]

population.table[ population.table[,5]=="meh"
                 | population.table[,5]=="good"
                 & population.table[,2]>3000,]

population.table[(population.table[,5]=="meh"
                  | population.table[,5]=="good")
                  & population.table[,2]>3000,]

population.table[population.table[,2]>3000
                 & (population.table[,5]=="meh"
                 | population.table[,5]=="good"),]

population.table[population.table[,2]>3000
                 & (population.table[,5] %in% c("meh","good")),]

population.table[population.table[,4] < median(population.table[,4]) 
                 &(population.table[,5]=="bad" | population.table[,5]=="meh")
                 & population.table[,3] <2,]

#Note that we've already loaded the rio package in the code above. We shouldn't have to load it again.
pa.counties <- import("https://github.com/marctrussler/IDS-Data/raw/main/PACounties.Rds")
head(pa.counties)

plot(pa.counties[,4], pa.counties[,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16)

#Rural
pa.counties[pa.counties[,2]<100,]

#Suburban-Urban
pa.counties[pa.counties[,2]>=100,]

#Uncomment and run:
#plot(pa.counties[pa.counties[,2]<100,4], pa.counties[,3],
#     xlab="Percent with College Degree",
#     ylab="Median Income",
#     main="College Degree Attainment vs. Household Income in PA",
#     pch=16)

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16)

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16)
points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=16)

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16)
points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=17)

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16,
     col="forestgreen")

points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=16,
       col="darkorange")

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16,
     col="forestgreen")

points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=16,
       col="darkorange")
legend("topleft",c("Rural","Suburban/Urban"), pch=c(16,16), col=c("forestgreen","orange"))

pa.counties[,4]<10

pa.counties[pa.counties[,4]<10,]

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA (Rural)",
     pch=16,
     col="forestgreen")

plot(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA (Non-Rural) ",
     pch=16,
     col="darkorange")

plot(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16,
     xlim=c(0,55),
     ylim=c(35000,100000),
     col="forestgreen")

points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=16,
       col="darkorange")
legend("topleft",c("Rural","Suburban/Urban"), pch=c(16,16), col=c("forestgreen","orange"))

plot(pa.counties[,4], pa.counties[,3],
     xlab="Percent with College Degree",
     ylab="Median Income",
     main="College Degree Attainment vs. Household Income in PA",
     pch=16, type="n")
points(pa.counties[pa.counties[,2]<100,4], pa.counties[pa.counties[,2]<100,3],
       pch=16,
       col="forestgreen")
points(pa.counties[pa.counties[,2]>=100,4], pa.counties[pa.counties[,2]>=100,3],
       pch=16,
       col="darkorange")

legend("topleft",c("Rural","Suburban/Urban"), pch=c(16,16), col=c("forestgreen","orange"))


plot(population.table[,2], population.table[,4], type="n",
     xlab="Population",
     ylab="Co2 Emissions",
     main="Co2 Emissions by Global Population")
points(population.table[population.table[,5]=="good",2],
       population.table[population.table[,5]=="good",4],
       col="forestgreen",
       pch=16)
points(population.table[population.table[,5]=="meh",2],
       population.table[population.table[,5]=="meh",4],
       col="darkorange",
       pch=16)
points(population.table[population.table[,5]=="bad",2],
       population.table[population.table[,5]=="bad",4],
       col="firebrick",
       pch=16)
legend("topleft", c("Good Music Decade", "Meh Music Decade", "Bad Music Decade"),
       pch=c(16,16,16),
       col=c("forestgreen","darkorange","firebrick"))
