Queer European MD passionate about IT

Mission518Solutions.Rmd 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. ---
  2. title: 'Data Structures in R: Guided Project Solutions'
  3. author: "Dataquest"
  4. date: "6/6/2020"
  5. output: html_document
  6. ---
  7. # Understanding the Data
  8. ## Loading the dataset from the `covid19.csv` CSV file and quick exploration
  9. ```{r}
  10. library(readr)
  11. # Loading the dataset
  12. covid_df <- read_csv("covid19.csv")
  13. ```
  14. ```{r}
  15. # Displaing the dimension of the data:
  16. dim(covid_df)
  17. # Storing the column names in a variable
  18. vector_cols <- colnames(covid_df)
  19. # Displaing the variable vector_cols
  20. vector_cols
  21. # Showing the first few rows of the dataset
  22. head(covid_df)
  23. # Showing a global view of the dataset.
  24. library(tibble)
  25. glimpse(covid_df)
  26. ```
  27. 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`.
  28. 1. This variable contains a character vector.
  29. 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()`.
  30. # Isolating the Data We Need
  31. ## Selecting only the rows related to `"All States"` and removing the `Province_State`.
  32. ```{r}
  33. library(dplyr)
  34. # Filter the "All States" Province states and remove the `Province_State` column
  35. covid_df_all_states <- covid_df %>%
  36. filter(Province_State == "All States") %>%
  37. select(-Province_State)
  38. ```
  39. ## Creating a dataset for the cumulative columns and another for the daily columns from `covid_df_all_states` dataframe
  40. Let's recall the description of the dataset's columns.
  41. 1. `Date`: Date
  42. 2. `Continent_Name`: Continent names
  43. 3. `Two_Letter_Country_Code`: Country codes
  44. 4. `Country_Region`: Country names
  45. 5. `Province_State`: States/province names; value is `All States` when state/provincial level data is not available
  46. 6. `positive`: Cumulative number of positive cases reported.
  47. 7. `active`: Number of actively cases on that **day**.
  48. 8. `hospitalized`: Cumulative number of hospitalized cases reported.
  49. 9. `hospitalizedCurr`: Number of actively hospitalized cases on that **day**.
  50. 10. `recovered`: Cumulative number of recovered cases reported.
  51. 11. `death`: Cumulative number of deaths reported.
  52. 12. `total_tested`: Cumulative number of tests conducted.
  53. 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.
  54. 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.
  55. ```{r}
  56. # Selecting the columns with cumulative numbers
  57. covid_df_all_states_cumulative <- covid_df_all_states %>%
  58. select(Date, Continent_Name, Two_Letter_Country_Code, positive, hospitalized, recovered, death, total_tested)
  59. # Selecting the columns with cumulative numbers
  60. covid_df_all_states_daily <- covid_df_all_states %>%
  61. select(Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive)
  62. ##print(xtable::xtable(head(covid_df_all_states_daily)), type = "html")
  63. ```
  64. 1. We can remove `Province_State` without loosing information because after the filtering step this column only contains the value `"All States"`.
  65. # Identifying the Highest Fatality Rates Countries
  66. ## Summarizing the data based on the `Continent_Name` and `Two_Letter_Country_Code` columns.
  67. ```{r}
  68. covid_df_all_states_cumulative_max <- covid_df_all_states_cumulative %>%
  69. group_by(Continent_Name, Two_Letter_Country_Code) %>%
  70. summarise(max = max(death)) %>%
  71. filter(max > 0)
  72. covid_df_all_states_cumulative_max
  73. ```
  74. ## Displaying the maximum number of death by country, colored by continent
  75. ```{r}
  76. library(ggplot2)
  77. qplot(x = Two_Letter_Country_Code,
  78. y = max,
  79. col = Continent_Name,
  80. data = covid_df_all_states_cumulative_max)
  81. ```
  82. ## Conclusion: Answering the question: Which countries have had the highest fatality (mortality) rates?
  83. ```{r}
  84. death_top_3 <- c("US", "IT", "GB")
  85. ```
  86. # Extracting the Top Ten countries in the number of tested cases
  87. ## Summarizing the data based on the `Country_Region` column.
  88. ```{r}
  89. covid_df_all_states_daily_sum <- covid_df_all_states_daily %>%
  90. group_by(Country_Region) %>%
  91. summarise(tested = sum(daily_tested),
  92. positive = sum(daily_positive),
  93. active = sum(active),
  94. hospitalized = sum(hospitalizedCurr)) %>%
  95. arrange(desc(tested)) #this is equivalent to `arrange(-tested)`
  96. covid_df_all_states_daily_sum
  97. #Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive
  98. ```
  99. ## Taking the top 10
  100. ```{r}
  101. covid_top_10 <- head(covid_df_all_states_daily_sum, 10)
  102. #print(xtable::xtable(covid_top_10), type = "html")
  103. ```
  104. # Identifying the Highest Positive Against Tested Cases
  105. ## Getting vectors
  106. ```{r}
  107. countries <- covid_top_10$Country_Region
  108. tested_cases <- covid_top_10$tested
  109. positive_cases <- covid_top_10$positive
  110. active_cases <- covid_top_10$active
  111. hospitalized_cases <- covid_top_10$hospitalized
  112. ```
  113. ## Naming vectors
  114. ```{r}
  115. names(positive_cases) <- countries
  116. names(tested_cases) <- countries
  117. names(active_cases) <- countries
  118. names(hospitalized_cases) <- countries
  119. ```
  120. ## Identifying
  121. ```{r}
  122. positive_cases
  123. sum(positive_cases)
  124. mean(positive_cases)
  125. positive_cases/sum(positive_cases)
  126. ```
  127. ```{r}
  128. positive_cases/tested_cases
  129. ```
  130. ## Conclusion
  131. ```{r}
  132. positive_tested_top_3 <- c("United Kingdom" = 0.11, "United States" = 0.10, "Turkey" = 0.08)
  133. ```
  134. # Identifying Affected Countries Related to their Population
  135. ```{r}
  136. # Creating the matrix covid_mat
  137. covid_mat <- cbind(tested_cases, positive_cases, active_cases, hospitalized_cases)
  138. # Creating the population vector https://www.worldometers.info/world-population/population-by-country/
  139. population <- c(331002651, 145934462, 60461826, 1380004385, 84339067, 37742154, 67886011, 25499884, 32971854, 37846611)
  140. # Dividing the matrix by the population vector
  141. covid_mat <- covid_mat * 100/population
  142. covid_mat
  143. ```
  144. ## Ranking the matrix
  145. ```{r}
  146. tested_cases_rank <- rank(covid_mat[,"tested_cases"])
  147. positive_cases_rank <- rank(covid_mat[,"positive_cases"])
  148. active_cases_rank <- rank(covid_mat[,"active_cases"])
  149. hospitalized_cases_rank <- rank(covid_mat[,"hospitalized_cases"])
  150. covid_mat_rank <- rbind(tested_cases_rank, positive_cases_rank, active_cases_rank, hospitalized_cases_rank)
  151. covid_mat_rank
  152. covid_mat_rank[1,]
  153. covid_mat_rank[-1, ]
  154. colSums(covid_mat_rank[-1, ])
  155. ```
  156. ## Conclusion
  157. ```{r}
  158. best_effort_tested_cased_top_3 <- c("India", "United Kingdom", "Turkey")
  159. most_affected_country <- "Italy"
  160. least_affected_country <- "India"
  161. ```
  162. # Putting all together
  163. ```{r}
  164. question_list <- list(
  165. "Which countries have had the highest fatality (mortality) rates?",
  166. "Which countries have had the highest number of positive cases against the number of tests?",
  167. "Which countries have made the best effort in terms of the number of tests conducted related to their population?",
  168. "Which countries were ultimately the most and least affected related to their population?"
  169. )
  170. answer_list <- list(
  171. "Death" = death_top_3,
  172. "Positive tested cases" = positive_tested_top_3,
  173. "The Best effort in test related to the population" = best_effort_tested_cased_top_3,
  174. "The most affected country related to its population" = most_affected_country,
  175. "The least affected country related to its population" = least_affected_country
  176. )
  177. answer_list
  178. datasets <- list(
  179. original = covid_df,
  180. allstates = covid_df_all_states,
  181. cumulative = covid_df_all_states_cumulative,
  182. daily = covid_df_all_states_daily
  183. )
  184. matrices <- list(covid_mat, covid_mat_rank)
  185. vectors <- list(vector_cols, population, countries)
  186. data_structure_list <- list("data frame" = datasets, "matrix" = matrices, "vector" = vectors)
  187. covid_analysis_list <- list(question_list, answer_list, data_structure_list)
  188. ```