Project 2

tidyr

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(stringr)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(anytime) 
## Warning: package 'anytime' was built under R version 4.3.3

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

Load three wide datasets .CSV

use tidyr and dplyr as needed to tidy and transform your data. [Most of your grade will be based on this step!]

Data Set 1 (CPI)

Data Set 1 (CPI) Load:

CPI <- read.csv("https://raw.githubusercontent.com/GuillermoCharlesSchneider/DATA-607/main/Project-2/CPI%202024.csv", sep =",")

Data Set 1 TIDYING (tidyr and dplyr):

#remove blanks
clean_CPI <- CPI %>%
  filter(!CPI$X=="")

#rename cols
  colnames(clean_CPI) <- c(
    "Indent Level",
    "Expenditure category",
    "Relative importance Dec. 2023",
    "Unadjusted indexes Jan. 2023",
    "Unadjusted indexes Dec. 2023",
    "Unadjusted indexes Jan. 2024",
    "Unadjusted percent change Jan. 2023- Jan. 2024",
    "Unadjusted percent change Dec. 2023- Jan. 2024",
    "Seasonally adjusted percent change Oct. 2023- Nov. 2023",
    "Seasonally adjusted percent change Nov. 2023- Dec. 2023",
    "Seasonally adjusted percent change Dec. 2023- Jan. 2024"
    )

clean_CPI <- data.frame(clean_CPI[-1, ])

#pivot to long form
long_CPI <- clean_CPI %>%
  pivot_longer(
    3:11,
    names_to = "indexes", 
    values_to = "value"
  )

Data Set 1 ANALYSIS:

#filter for just jan 23 to jan 24 index name
graph1 <- long_CPI %>%
  filter(long_CPI$indexes=="Unadjusted.percent.change.Jan..2023..Jan..2024")

#tilted the text for readability
graph1 %>% 
  ggplot(aes(x=reorder(Expenditure.category,as.numeric(value)),
             y=as.numeric(value)))+
  geom_col()+
   theme(axis.text.x = element_text(angle=65, vjust=1, hjust=1))+
ggtitle("Unadjusted Percent Change Jan 2023 to Jan 2024") +
  xlab("Expenditure Category") + ylab("Percent Change")

#filtered for dec 23 to jan 24 index name
graph2 <- long_CPI %>%
  filter(long_CPI$indexes=="Seasonally.adjusted.percent.change.Dec..2023..Jan..2024")

#again tilted for readability
graph2 %>% 
  ggplot(aes(x=reorder(Expenditure.category,as.numeric(value)),
             y=as.numeric(value)))+
  geom_col()+
   theme(axis.text.x = element_text(angle=65, vjust=1, hjust=1))+
ggtitle("Seasonally Adjusted Percent Change Dec 2023 to Jan 2024") +
  xlab("Expenditure Category") + ylab("Percent Change")

Question: “Which spending categories have experienced the most inflation and the least? In the last month and over the whole year?”

Answer:

Over the whole year, Jan 2023 to Jan 2024, Motor Vehicle Insurance experienced the most inflation (20.6%), and Utility (piped) Gas Service experienced the least (-17.8%).

Over the last month of 2023, Dec 2023 to Jan 2024 (Seasonally adjusted), Utility (piped) Gas Service experienced the most inflation (2.0%), and Fuel oil experienced the least (-4.5%).

Data Set 2 (Marriage)

Data Set 2 (Marriage) Load:

Marriage <- read.csv("https://raw.githubusercontent.com/GuillermoCharlesSchneider/DATA-607/main/Project-2/Marriage.csv", sep =",")

Data Set 2 TIDYING (tidyr and dplyr):

Clean_Marriage <- data.frame(Gender = "Male", Marriage[-c(1:2,17:37), ])
Clean_Marriage$Gender[c(8:14)] <- "Female"
Clean_Marriage <- data.frame(Clean_Marriage[-c(1,8), ])


Long_Marriage <- Clean_Marriage %>%
  pivot_longer(
    3:14,
    names_to = "marriage", 
    values_to = "value"
  )

Data Set 2 ANALYSIS

#never married female, remove %
Female_Never_Married <- Long_Marriage %>%
  filter(Long_Marriage$Gender == "Female", Long_Marriage$marriage == "United.States..Never.married..Estimate")

Female_Never_Married$value <- str_remove_all(Female_Never_Married$value,"[\\%]")

Female_Never_Married$Change <- NA

# change in brackets
for (i in 2:6){
  Female_Never_Married$Change[i] <- as.numeric(Female_Never_Married$value[i]) - as.numeric(Female_Never_Married$value[i-1])
}

#never married male, remove %
Male_Never_Married <- Long_Marriage %>%
  filter(Long_Marriage$Gender == "Male", Long_Marriage$marriage == "United.States..Never.married..Estimate")
  
Male_Never_Married$value <- str_remove_all(Male_Never_Married$value,"[\\%]")

Male_Never_Married$Change <- NA

# change in brackets
for (i in 2:6){
  Male_Never_Married$Change[i] <- as.numeric(Male_Never_Married$value[i]) - as.numeric(Male_Never_Married$value[i-1])
}

#comparing change in age brackets from male to female
Male_Never_Married$Male_compared_to_female <- NA

for (i in 2:6){
  Male_Never_Married$Male_compared_to_female[i] <- as.numeric(Male_Never_Married$Change[i]) - as.numeric(Female_Never_Married$Change[i])
}

Question: “In 2022 when this data was collected, what age bracket for males and females is the change in the proportion that have ever been married the largest?”

Answer:

Largest difference between age brackets in “Males Never Married” is from the “20 to 34 years” age bracket (71.3%) to the “35 to 44 years” age bracket (29.3%), a change of -42.0%

Largest difference between age bracket in “Females Never Married” is from the “20 to 34 years” age bracket (63.5%) to the “35 to 44 years” age bracket (23.6%), a change of -39.9%

The change from the “15 to 19 years” age bracket to the “20 to 34 years” age bracket has the largest gap between Males (-27.6%) and Females (-35.2%) at 7.6%

Data Set 3 (Sheds)

Data Set 3 (Sheds) Load:

Sheds <- read.csv("https://raw.githubusercontent.com/GuillermoCharlesSchneider/DATA-607/main/Project-2/Sheds.csv", sep =",")

Data Set 3 TIDYING (tidyr and dplyr):

Shed_Clean <- Sheds

Sheds_Clean <- data.frame(Sheds[,-3])

# trying unsuccessfully to fix the date format inconsistency w lubridate package?
#Sheds_Clean$First.Permit.Date[c(1:114)] <- mdy(Sheds_Clean$First.Permit.Date[c(1:114)])
#Sheds_Clean$First.Permit.Date[c(1:114)] <- format(Sheds_Clean$First.Permit.Date[c(1:114)], format = "%Y-%m-%d")

# i think this "anytime" package worked better than lubridate
Sheds_Clean$First.Permit.Date <- anydate(Sheds_Clean$First.Permit.Date)
Sheds_Clean$Permit.Expiration.Date <- anydate(Sheds_Clean$Permit.Expiration.Date)

Sheds_Age <- data.frame(Sheds_Clean[,c(1,3,5,6,19)]) %>%
arrange(desc(Age))

Data Set 3 ANALYSIS

Question: This data can be analyzed to see how many sheds each company has applied to, this will also give us the ability to identify landlords who are negligent in repairing their properties based on how long these sheds have been present at the locations

How many sheds each company has:

Shed_Company_Count <- Sheds %>% group_by(Applicant.Business.Name) %>% 
  summarise(total_count=n(),
            .groups = 'drop') %>%
  arrange(desc(total_count))


Shed_Company_Count$Percent <- NA

for (i in 1:161){
  Shed_Company_Count$Percent[i] <- as.numeric(Shed_Company_Count$total_count[i])/ sum(as.numeric(Shed_Company_Count$total_count))*100
}

print(Shed_Company_Count[1:10,])
## # A tibble: 10 × 3
##    Applicant.Business.Name      total_count Percent
##    <chr>                              <int>   <dbl>
##  1 ASHRAF CORP                         1262   15.0 
##  2 ENTHINK ENGINEERING LLC              982   11.7 
##  3 PAUL PERDEK  PROF. ENG. PLLC         571    6.78
##  4 PR                                   477    5.66
##  5 ASR ENGINEERING PC                   418    4.96
##  6 JOEL PHAGOO P.E. PLLC                364    4.32
##  7 MJE PROFESSIONAL SERVICES            341    4.05
##  8 MOHAMMAD AHEAD  PE                   311    3.69
##  9 KZ ENGINEERING  PC                   299    3.55
## 10 GEIGER E&A GROUP CORP.               207    2.46

long these sheds have been present at the locations:

#oldest sheds
print(Sheds_Age[1:10,])
##    Job.Number First.Permit.Date  Age Permit.Expiration.Date
## 1   104416464        2006-04-27 6512             2024-06-25
## 2   410117350        2008-11-19 5575             2024-03-21
## 3   120351662        2010-05-13 5035             2024-05-18
## 4   120486633        2010-09-29 4896             2024-09-10
## 5   320254817        2011-01-05 4798             2024-11-05
## 6   320254826        2011-01-05 4798             2024-06-22
## 7   120725705        2011-06-16 4636             2024-02-29
## 8   420516873        2011-12-20 4449             2024-04-06
## 9   220161278        2011-12-23 4446             2024-08-20
## 10  120987236        2012-03-01 4377             2024-06-11
##      Applicant.Business.Name
## 1    ROCKLEDGE SCAFFOLD CORP
## 2      CUSTOMBUILT HOMES INC
## 3             CS BRIDGE CORP
## 4    ROCKLEDGE SCAFFOLD CORP
## 5     PICCO CONSTRUCTION LLC
## 6         PJP INSTALLERS INC
## 7               BS GROUP INC
## 8  TRIANGLE ENTERPRISE NYC I
## 9     SASCO CONSTRUCTION ENT
## 10      ARSENAL SCAFFOLD INC