-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Mission518Solutions.Rmd
265 lines (189 loc) · 7.86 KB
/
Mission518Solutions.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
---
title: 'Data Structures in R: Guided Project Solutions'
author: "Dataquest"
date: "6/6/2020"
output: html_document
---
# Understanding the Data
## Loading the dataset from the `covid19.csv` CSV file and quick exploration
```{r}
library(readr)
# Loading the dataset
covid_df <- read_csv("covid19.csv")
```
```{r}
# Displaing the dimension of the data:
dim(covid_df)
# Storing the column names in a variable
vector_cols <- colnames(covid_df)
# Displaing the variable vector_cols
vector_cols
# Showing the first few rows of the dataset
head(covid_df)
# Showing a global view of the dataset.
library(tibble)
glimpse(covid_df)
```
The dataset contains `14` columns and `10,903` rows. This database provides information on the numbers (per day and cumulatively) of COVID-19 positive cases, deaths, tests performed and hospitalizations for each country through the column's names store in the variable `vector_cols`.
1. This variable contains a character vector.
2. The use of the function `glimpse()` is the very first operation to do because we don't only learn about the dimensions of the database but also about the names of the first columns and their types and content. It can replace the three previous operations: `dim()`, `colnames()`, and `head()`.
# Isolating the Data We Need
## Selecting only the rows related to `"All States"` and removing the `Province_State`.
```{r}
library(dplyr)
# Filter the "All States" Province states and remove the `Province_State` column
covid_df_all_states <- covid_df %>%
filter(Province_State == "All States") %>%
select(-Province_State)
```
## Creating a dataset for the cumulative columns and another for the daily columns from `covid_df_all_states` dataframe
Let's recall the description of the dataset's columns.
1. `Date`: Date
2. `Continent_Name`: Continent names
3. `Two_Letter_Country_Code`: Country codes
4. `Country_Region`: Country names
5. `Province_State`: States/province names; value is `All States` when state/provincial level data is not available
6. `positive`: Cumulative number of positive cases reported.
7. `active`: Number of actively cases on that **day**.
8. `hospitalized`: Cumulative number of hospitalized cases reported.
9. `hospitalizedCurr`: Number of actively hospitalized cases on that **day**.
10. `recovered`: Cumulative number of recovered cases reported.
11. `death`: Cumulative number of deaths reported.
12. `total_tested`: Cumulative number of tests conducted.
13. `daily_tested`: Number of tests conducted on the **day**; if daily data is unavailable, daily tested is averaged across number of days in between.
14. `daily_positive`: Number of positive cases reported on the **day**; if daily data is unavailable, daily positive is averaged across number of days in.
```{r}
# Selecting the columns with cumulative numbers
covid_df_all_states_cumulative <- covid_df_all_states %>%
select(Date, Continent_Name, Two_Letter_Country_Code, positive, hospitalized, recovered, death, total_tested)
# Selecting the columns with cumulative numbers
covid_df_all_states_daily <- covid_df_all_states %>%
select(Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive)
##print(xtable::xtable(head(covid_df_all_states_daily)), type = "html")
```
1. We can remove `Province_State` without loosing information because after the filtering step this column only contains the value `"All States"`.
# Identifying the Highest Fatality Rates Countries
## Summarizing the data based on the `Continent_Name` and `Two_Letter_Country_Code` columns.
```{r}
covid_df_all_states_cumulative_max <- covid_df_all_states_cumulative %>%
group_by(Continent_Name, Two_Letter_Country_Code) %>%
summarise(max = max(death)) %>%
filter(max > 0)
covid_df_all_states_cumulative_max
```
## Displaying the maximum number of death by country, colored by continent
```{r}
library(ggplot2)
gglot(data = covid_df_all_states_cumulative_max,
aes(x = Two_Letter_Country_Code,
y = max,
col = Continent_Name)) +
geom_point()
```
## Conclusion: Answering the question: Which countries have had the highest fatality (mortality) rates?
```{r}
death_top_3 <- c("US", "IT", "GB")
```
# Extracting the Top Ten countries in the number of tested cases
## Summarizing the data based on the `Country_Region` column.
```{r}
covid_df_all_states_daily_sum <- covid_df_all_states_daily %>%
group_by(Country_Region) %>%
summarise(tested = sum(daily_tested),
positive = sum(daily_positive),
active = sum(active),
hospitalized = sum(hospitalizedCurr)) %>%
arrange(desc(tested)) #this is equivalent to `arrange(-tested)`
covid_df_all_states_daily_sum
#Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive
```
## Taking the top 10
```{r}
covid_top_10 <- head(covid_df_all_states_daily_sum, 10)
#print(xtable::xtable(covid_top_10), type = "html")
```
# Identifying the Highest Positive Against Tested Cases
## Getting vectors
```{r}
countries <- covid_top_10$Country_Region
tested_cases <- covid_top_10$tested
positive_cases <- covid_top_10$positive
active_cases <- covid_top_10$active
hospitalized_cases <- covid_top_10$hospitalized
```
## Naming vectors
```{r}
names(positive_cases) <- countries
names(tested_cases) <- countries
names(active_cases) <- countries
names(hospitalized_cases) <- countries
```
## Identifying
```{r}
positive_cases
sum(positive_cases)
mean(positive_cases)
positive_cases/sum(positive_cases)
```
```{r}
positive_cases/tested_cases
```
## Conclusion
```{r}
positive_tested_top_3 <- c("United Kingdom" = 0.11, "United States" = 0.10, "Turkey" = 0.08)
```
# Identifying Affected Countries Related to their Population
```{r}
# Creating the matrix covid_mat
covid_mat <- cbind(tested_cases, positive_cases, active_cases, hospitalized_cases)
# Creating the population vector https://www.worldometers.info/world-population/population-by-country/
population <- c(331002651, 145934462, 60461826, 1380004385, 84339067, 37742154, 67886011, 25499884, 32971854, 37846611)
# Dividing the matrix by the population vector
covid_mat <- covid_mat * 100/population
covid_mat
```
## Ranking the matrix
```{r}
tested_cases_rank <- rank(covid_mat[,"tested_cases"])
positive_cases_rank <- rank(covid_mat[,"positive_cases"])
active_cases_rank <- rank(covid_mat[,"active_cases"])
hospitalized_cases_rank <- rank(covid_mat[,"hospitalized_cases"])
covid_mat_rank <- rbind(tested_cases_rank, positive_cases_rank, active_cases_rank, hospitalized_cases_rank)
covid_mat_rank
covid_mat_rank[1,]
covid_mat_rank[-1, ]
colSums(covid_mat_rank[-1, ])
```
## Conclusion
```{r}
best_effort_tested_cased_top_3 <- c("India", "United Kingdom", "Turkey")
most_affected_country <- "Italy"
least_affected_country <- "India"
```
# Putting all together
```{r}
question_list <- list(
"Which countries have had the highest fatality (mortality) rates?",
"Which countries have had the highest number of positive cases against the number of tests?",
"Which countries have made the best effort in terms of the number of tests conducted related to their population?",
"Which countries were ultimately the most and least affected related to their population?"
)
answer_list <- list(
"Death" = death_top_3,
"Positive tested cases" = positive_tested_top_3,
"The Best effort in test related to the population" = best_effort_tested_cased_top_3,
"The most affected country related to its population" = most_affected_country,
"The least affected country related to its population" = least_affected_country
)
answer_list
datasets <- list(
original = covid_df,
allstates = covid_df_all_states,
cumulative = covid_df_all_states_cumulative,
daily = covid_df_all_states_daily
)
matrices <- list(covid_mat, covid_mat_rank)
vectors <- list(vector_cols, population, countries)
data_structure_list <- list("data frame" = datasets, "matrix" = matrices, "vector" = vectors)
covid_analysis_list <- list(question_list, answer_list, data_structure_list)
```