Package used
library(tidyverse)
library(ggplot2)
library(dplyr)
1. Import the bomber_wide.rds file, which lists the flying hours for each aircraft by year. Convert this data to a tibble and tidy it by changing it from a wide format to a long format so that you have the following columns: Type, MD, Year, & FH. The final data should look like:
bombw<-readRDS("bomber_wide.rds")
t1<-as_tibble(bombw)
t2<-gather(
t1,
Year,
FH,
`1996`:`2014`,
factor_key = TRUE
)
2. Import the bomber_long.rds data, which provides the value for three different outputs for each aircraft by year. The output measures include cost, flying hours, and gallons of gas consumed but these variables are “stacked” in the Output variable. Change this data to a tibble and convert to a wider format so that you have the following columns: Type, MD, FY, Cost, FH, & Gallons. Your data should look like:
bomln<-readRDS("bomber_long.rds")
t3<-as_tibble(bomln)
t4<-spread(t3,Output,Value)
t4
## # A tibble: 57 × 6
## Type MD FY Cost FH Gallons
## * <chr> <chr> <int> <int> <int> <int>
## 1 Bomber B-1 1996 72753781 26914 88594449
## 2 Bomber B-1 1997 71297263 25219 85484074
## 3 Bomber B-1 1998 84026805 24205 85259038
## 4 Bomber B-1 1999 71848336 23306 79323816
## 5 Bomber B-1 2000 58439777 25013 86230284
## 6 Bomber B-1 2001 94946077 25059 86892432
## 7 Bomber B-1 2002 96458536 26581 89198262
## 8 Bomber B-1 2003 68650070 21491 74485788
## 9 Bomber B-1 2004 101895634 28118 101397707
## 10 Bomber B-1 2005 124816690 21859 78410415
## # ... with 47 more rows
3.Import the bomber_combined.rds file. Note that the first variable in this data (AC) combines the aircraft type (Bomber) and aircraft designator (i.e. B-1). This variable should be split into two. Take this data and convert it to a tibble and separate the AC variable into “Type” and “MD” so that your data looks like:
bomcom<-readRDS("bomber_combined.rds")
t5<-as_tibble(bomcom)
t6<-separate(t5,AC,c("TYPE","MD"),sep=" ")
t6
## # A tibble: 57 × 6
## TYPE MD FY Cost FH Gallons
## * <chr> <chr> <int> <int> <int> <int>
## 1 Bomber B-1 1996 72753781 26914 88594449
## 2 Bomber B-1 1997 71297263 25219 85484074
## 3 Bomber B-1 1998 84026805 24205 85259038
## 4 Bomber B-1 1999 71848336 23306 79323816
## 5 Bomber B-1 2000 58439777 25013 86230284
## 6 Bomber B-1 2001 94946077 25059 86892432
## 7 Bomber B-1 2002 96458536 26581 89198262
## 8 Bomber B-1 2003 68650070 21491 74485788
## 9 Bomber B-1 2004 101895634 28118 101397707
## 10 Bomber B-1 2005 124816690 21859 78410415
## # ... with 47 more rows
4. Import the bomber_prefix.rds data. Take this data and convert it to a tibble and unite the prefix and number variables into an “MD” variable so that the data matches the tidy data sets you produced in problems #2 and #3.
bompre<-readRDS("bomber_prefix.rds")
t7<-as_tibble(bompre)
t8<-unite(t7,MD,c(prefix,number),sep = "-")
t8
## # A tibble: 171 × 5
## Type MD FY Output Value
## * <chr> <chr> <int> <chr> <int>
## 1 Bomber B-1 1996 FH 26914
## 2 Bomber B-1 1997 FH 25219
## 3 Bomber B-1 1998 FH 24205
## 4 Bomber B-1 1999 FH 23306
## 5 Bomber B-1 2000 FH 25013
## 6 Bomber B-1 2001 FH 25059
## 7 Bomber B-1 2002 FH 26581
## 8 Bomber B-1 2003 FH 21491
## 9 Bomber B-1 2004 FH 28118
## 10 Bomber B-1 2005 FH 21859
## # ... with 161 more rows
5.Import the bomber_mess.rds file so that it is a tibble. Clean this data up by making it contain the following variables:
Type MD which combines the prefix and number variable (i.e. “B-1”) FY which is the left part of the Metric variable Cost which is captured in the right part of the Metric variable FH which is captured in the right part of the Metric variable Gallons which is captured in the right part of the Metric variable
bommess<-readRDS("bomber_mess.rds")
t9<-as_tibble(bommess)
t10<-t9 %>% unite(.,MD,c(prefix,number),sep="-") %>% separate(.,Metric,c("FY","Cost"),sep="_") %>% spread(Cost,Value)
t10
## # A tibble: 57 × 6
## Type MD FY Cost FH Gallons
## * <chr> <chr> <chr> <int> <int> <int>
## 1 Bomber B-1 1996 72753781 26914 88594449
## 2 Bomber B-1 1997 71297263 25219 85484074
## 3 Bomber B-1 1998 84026805 24205 85259038
## 4 Bomber B-1 1999 71848336 23306 79323816
## 5 Bomber B-1 2000 58439777 25013 86230284
## 6 Bomber B-1 2001 94946077 25059 86892432
## 7 Bomber B-1 2002 96458536 26581 89198262
## 8 Bomber B-1 2003 68650070 21491 74485788
## 9 Bomber B-1 2004 101895634 28118 101397707
## 10 Bomber B-1 2005 124816690 21859 78410415
## # ... with 47 more rows
Once you’ve created the above tidy data, plot the historical trends of this data in ggplot2 with a line chart such that the plot is facetted by the Cost, FH, and Gallons variables and each facet compares the different MDs (“B-1”, “B-2”, “B-52”).
ggplot(data=t10,aes(y=Cost,x=FH,colour=MD,group=MD))+
geom_line()+
facet_wrap(~MD)
ggplot(data=t10,aes(y=Cost,x=Gallons,colour=MD,group=MD))+
geom_line()+
facet_wrap(~MD)
6.Import the ws_programmatics.rds & ws_categorization.rds data so that they are tibbles and perform the following steps in sequence using the pipe operator (%>%).
Join the ws_categorization data to the ws_programmatics data Filter for only FY 2014 data at the following Base: Minot AFB (ND) Filter for only Systems classified as “AIRCRAFT” or “MISSILES” Group the data by System level Calculate the total sum of the Total_O.S and End_Strength variables
pro1<-as_tibble(readRDS("ws_programmatics.rds"))
cat1<-as_tibble(readRDS("ws_categorizations.rds"))
merge(pro1,cat1) %>% filter(FY==2014,Base=="MINOT AFB (ND)") %>%
filter(System %in% c("AIRCRAFT","MISSILES")) %>% group_by(System) %>% summarise(Total_OS=sum(Total_O.S,na.rm=TRUE),End_Strength_sum=sum(End_Strength,na.rm=TRUE))
## # A tibble: 2 × 3
## System Total_OS End_Strength_sum
## <chr> <dbl> <dbl>
## 1 AIRCRAFT 297398235 2023
## 2 MISSILES 112224909 1951
7. Once again, join the ws_programmatics.rds & ws_categorization.rds data; however, this time identify which Base had the largest cost per flying hour in 2014. Using a bar chart in ggplot2, plot these values for the top 10 bases with the largest cost per flying hour.
CPFH14<-merge(pro1,cat1) %>% filter(FY==2014) %>% group_by(Base) %>%
summarise(CPFH=(sum(Total_O.S,na.rm=TRUE)/sum(FH,na.rm=TRUE))) %>% filter(CPFH != 'Inf') %>% arrange(desc(CPFH))
CPFH14[CPFH14$CPFH == max(CPFH14$CPFH,na.rm = TRUE),]
## # A tibble: 2 × 2
## Base CPFH
## <chr> <dbl>
## 1 PATRICK AFB (FL) 178960
## 2 <NA> NA
ggplot(data=head(CPFH14,10),aes(x=reorder(Base,CPFH),y=CPFH)) +
geom_bar(stat="identity") +
coord_flip()
8. Using scatter plots in ggplot2, assess the relationship between the end strength (End_Strength) variable and total costs (Total_O.S). Provide three scatter plots that visually assesses this replationship from different angles (by FY, System, etc).
newpc<-as_tibble(merge(pro1,cat1))
ggplot(newpc, aes(x = Total_O.S, y = End_Strength)) +
geom_line() +
geom_point()
## Warning: Removed 6 rows containing missing values (geom_path).
## Warning: Removed 15487 rows containing missing values (geom_point).
ggplot(newpc, aes(x = Total_O.S, y =End_Strength ))+
geom_point()+
facet_wrap(~System)
## Warning: Removed 15487 rows containing missing values (geom_point).
ggplot(newpc, aes(x = Total_O.S, y =End_Strength ))+
geom_point()+
facet_wrap(~FY,nrow = 4)
## Warning: Removed 15487 rows containing missing values (geom_point).