Queer European MD passionate about IT

Mission518Solutions.Rmd 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. gglot(data = covid_df_all_states_cumulative_max,
  78. aes(x = Two_Letter_Country_Code,
  79. y = max,
  80. col = Continent_Name)) +
  81. geom_point()
  82. ```
  83. ## Conclusion: Answering the question: Which countries have had the highest fatality (mortality) rates?
  84. ```{r}
  85. death_top_3 <- c("US", "IT", "GB")
  86. ```
  87. # Extracting the Top Ten countries in the number of tested cases
  88. ## Summarizing the data based on the `Country_Region` column.
  89. ```{r}
  90. covid_df_all_states_daily_sum <- covid_df_all_states_daily %>%
  91. group_by(Country_Region) %>%
  92. summarise(tested = sum(daily_tested),
  93. positive = sum(daily_positive),
  94. active = sum(active),
  95. hospitalized = sum(hospitalizedCurr)) %>%
  96. arrange(desc(tested)) #this is equivalent to `arrange(-tested)`
  97. covid_df_all_states_daily_sum
  98. #Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive
  99. ```
  100. ## Taking the top 10
  101. ```{r}
  102. covid_top_10 <- head(covid_df_all_states_daily_sum, 10)
  103. #print(xtable::xtable(covid_top_10), type = "html")
  104. ```
  105. # Identifying the Highest Positive Against Tested Cases
  106. ## Getting vectors
  107. ```{r}
  108. countries <- covid_top_10$Country_Region
  109. tested_cases <- covid_top_10$tested
  110. positive_cases <- covid_top_10$positive
  111. active_cases <- covid_top_10$active
  112. hospitalized_cases <- covid_top_10$hospitalized
  113. ```
  114. ## Naming vectors
  115. ```{r}
  116. names(positive_cases) <- countries
  117. names(tested_cases) <- countries
  118. names(active_cases) <- countries
  119. names(hospitalized_cases) <- countries
  120. ```
  121. ## Identifying
  122. ```{r}
  123. positive_cases
  124. sum(positive_cases)
  125. mean(positive_cases)
  126. positive_cases/sum(positive_cases)
  127. ```
  128. ```{r}
  129. positive_cases/tested_cases
  130. ```
  131. ## Conclusion
  132. ```{r}
  133. positive_tested_top_3 <- c("United Kingdom" = 0.11, "United States" = 0.10, "Turkey" = 0.08)
  134. ```
  135. # Identifying Affected Countries Related to their Population
  136. ```{r}
  137. # Creating the matrix covid_mat
  138. covid_mat <- cbind(tested_cases, positive_cases, active_cases, hospitalized_cases)
  139. # Creating the population vector https://www.worldometers.info/world-population/population-by-country/
  140. population <- c(331002651, 145934462, 60461826, 1380004385, 84339067, 37742154, 67886011, 25499884, 32971854, 37846611)
  141. # Dividing the matrix by the population vector
  142. covid_mat <- covid_mat * 100/population
  143. covid_mat
  144. ```
  145. ## Ranking the matrix
  146. ```{r}
  147. tested_cases_rank <- rank(covid_mat[,"tested_cases"])
  148. positive_cases_rank <- rank(covid_mat[,"positive_cases"])
  149. active_cases_rank <- rank(covid_mat[,"active_cases"])
  150. hospitalized_cases_rank <- rank(covid_mat[,"hospitalized_cases"])
  151. covid_mat_rank <- rbind(tested_cases_rank, positive_cases_rank, active_cases_rank, hospitalized_cases_rank)
  152. covid_mat_rank
  153. covid_mat_rank[1,]
  154. covid_mat_rank[-1, ]
  155. colSums(covid_mat_rank[-1, ])
  156. ```
  157. ## Conclusion
  158. ```{r}
  159. best_effort_tested_cased_top_3 <- c("India", "United Kingdom", "Turkey")
  160. most_affected_country <- "Italy"
  161. least_affected_country <- "India"
  162. ```
  163. # Putting all together
  164. ```{r}
  165. question_list <- list(
  166. "Which countries have had the highest fatality (mortality) rates?",
  167. "Which countries have had the highest number of positive cases against the number of tests?",
  168. "Which countries have made the best effort in terms of the number of tests conducted related to their population?",
  169. "Which countries were ultimately the most and least affected related to their population?"
  170. )
  171. answer_list <- list(
  172. "Death" = death_top_3,
  173. "Positive tested cases" = positive_tested_top_3,
  174. "The Best effort in test related to the population" = best_effort_tested_cased_top_3,
  175. "The most affected country related to its population" = most_affected_country,
  176. "The least affected country related to its population" = least_affected_country
  177. )
  178. answer_list
  179. datasets <- list(
  180. original = covid_df,
  181. allstates = covid_df_all_states,
  182. cumulative = covid_df_all_states_cumulative,
  183. daily = covid_df_all_states_daily
  184. )
  185. matrices <- list(covid_mat, covid_mat_rank)
  186. vectors <- list(vector_cols, population, countries)
  187. data_structure_list <- list("data frame" = datasets, "matrix" = matrices, "vector" = vectors)
  188. covid_analysis_list <- list(question_list, answer_list, data_structure_list)
  189. ```