-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project2.Rmd
238 lines (161 loc) · 5.84 KB
/
Project2.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
title: "Week 6 Project 2"
author: "MG"
date: "10/3/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# Dataset 1
This data set is from Shana's Airline Safety post.
The data is located at: https://raw.githubusercontent.com/fivethirtyeight/data/master/airline-safety/airline-safety.csv
The analysis asked for is to sort and compare the number of airlines and their fatalities given to see any trends there.
The first step is to import the csv file from the website where the data is located and create a dataset out of it.
```{r}
df <- read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/airline-safety/airline-safety.csv", header = TRUE)
head(df)
df2 <- as_tibble(df)
head(df2)
```
#### Creating new data columns to consolidate the data
I'm creating 3 new columns to aggregate the data. Total Incidents, Total Fatal Accidents and Total Fatalities are the three new columns created with the mutate function.
```{r}
df3 <- mutate(df2, totalIncidents = (incidents_85_99 + incidents_00_14), totalAccidents = (fatal_accidents_85_99 + fatal_accidents_00_14), totalFatalities = (fatalities_85_99 + fatalities_00_14))
head(df3)
```
Now that we have the new columns of data, we will now transform it from "wide" to something that is easier the use in **R**.
```{r}
df4 <- gather(df3, "Situations", "Situation Count", 3:11)
head(df4, 10)
```
Now that we had the data in a "long" format in **R**, we'll look to answer the questions. First we will sort it by airline as requested. Then we'll pull out the data for Total Fatalities.
```{r}
df4 %>%
arrange(`airline`)
df4 %>%
filter(`Situations` == "totalFatalities")
```
Now that we have the Total Fatalities pulled out, we'll graph the data to see which airlines have alot of fatalities and which airlines have very few fatalities.
```{r}
df5 <-
select(df4, "airline", "Situation Count") %>%
rename("Fatalities" = "Situation Count",
"Airlines" = "airline")
head(df5)
#We have the data but now we'll reorder it so it's hightest fatalities to least fatalities
#df5$Airlines <-
#fct_rev(df5$Airlines)
ggplot(data = df5, aes(y = Airlines, x = Fatalities)) +
geom_line() + theme(axis.text = element_text(color = "grey50", size = 4))
```
The graph shows that China airlines had the most fatalities by far.
# Dataset 2
This data set is from Rachel's "Who eats the food we grow?" post.
The data is located at: https://www.kaggle.com/dorbicycle/world-foodfeed-production
https://www.kaggle.com/dorbicycle/world-foodfeed-production/download
But since you have to login to get the data, I have also placed it at:
https://github.com/mjgons/DATA607/blob/master/FAO.csv
The question to answer was, "I think the most interesting thing to do would be to figure out if feed has overtaken food in any areas or food items."
The first step is to import the csv file from the website where the data is located and create a dataset out of it.
```{r}
data1 <- read.csv("FAO.csv", header = TRUE)
#converting it to a tibble
data2 <- as_tibble(data1)
head(data2)
```
Now that we have the data, we need to convert it from "wide" to "long" to be able to do analysis on it in **R**. I'm using the "pivot_longer" function this time instead of the "gather" function.
```{r}
data3 <-
data2 %>%
pivot_longer(
starts_with("y"),
names_to = "year",
values_to = "tonnes1k"
)
head(data3)
```
Now we will separate the year to remove the leading "Y" from the year.
```{r}
data4 <-
data3 %>%
separate(
col = year,
into = c("Y", "year"),
sep = "Y",
convert = TRUE
)
head(data4)
```
Let's now remove columns we don't need in the dataset
```{r}
data5 <- data4
data5 <- select(data4, -1:-2)
data5 <- select(data5, -2:-4)
data5 <- select(data5, -3:-5)
data5 <- select(data5, -3)
head(data5)
summary(data5)
```
Now, let's plot Food and Feed and see the overall amount produced by year.
```{r}
ggplot(data5, aes(x = year, y = tonnes1k)) +
geom_line()
```
Now lets look at a specific country
```{r}
data6 <- data5
data6 <- filter(data6, Area == "Afghanistan", Element == "Feed")
ggplot(data6, aes(x = year, y = tonnes1k)) +
geom_line()
```
```{r}
data7 <- data5
data7 <- filter(data7, Area == "Afghanistan", Element == "Food")
ggplot(data7, aes(x = year, y = tonnes1k)) +
geom_line()
```
We can see for Afghanistan that in no year did Feed overtake Food.
```{r}
```
#Dataset 3
This dataset is based on post by Richard on Broadband Services within NYS.
The dataset is located at: https://data.ny.gov/api/views/sjc6-ftj4/rows.csv
The analysis asked to look at the municipalities that have lower rates of broadband and the number of housing units affected.
The first step is to import the csv file from the website where the data is located and create a dataset out of it.
```{r}
nys1 <- read.csv("https://data.ny.gov/api/views/sjc6-ftj4/rows.csv", header = TRUE)
head(nys1)
nys2 <- as_tibble(nys1)
head(nys2)
```
Now we'll remove the columns not needed.
```{r}
nys3 <- nys2
nys3 <- select(nys3, (-24))
nys3 <- select(nys3, (-23))
nys3 <- select(nys3, (-21))
nys3 <- select(nys3, (-20))
nys3 <- select(nys3, (-18))
nys3 <- select(nys3, (-17))
nys3 <- select(nys3, (-15))
nys3 <- select(nys3, (-14))
nys3 <- select(nys3, (-12))
nys3 <- select(nys3, (-11))
nys3 <- select(nys3, (-9))
head(nys3)
```
Now we are going to find the maximum number of broadband households in each city/town/village.
```{r}
nys4 <- nys3
nys4 %>%
rowwise() %>%
mutate(max = max(X..Hse.Units.Cable, X..Hse.Units.DSL, X..Hse.Units.Fiber, X..Hse.Units.Wireline, X..Hse.Units.Wireless))
head(nys4)
```
Now that we have the maximum value by area we can now calculate the broadband percentage by area.
```{r}
#nys5 <- nys4 %>%
# mutate("BBpct" = (`max` / `X2010.Muni.Population`))
```