Queer European MD passionate about IT

Mission505Solutions.Rmd 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Rows 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. - We can remove `Province_State` without loosing information because after the filtering step this column only contains the value `"All States"`.
  40. # Isolating the Columns We Need
  41. - Creating a dataset for the daily columns from `covid_df_all_states` dataframe
  42. Let's recall the description of the dataset's columns.
  43. 1. `Date`: Date
  44. 2. `Continent_Name`: Continent names
  45. 3. `Two_Letter_Country_Code`: Country codes
  46. 4. `Country_Region`: Country names
  47. 5. `Province_State`: States/province names; value is `All States` when state/provincial level data is not available
  48. 6. `positive`: Cumulative number of positive cases reported.
  49. 7. `active`: Number of actively cases on that **day**.
  50. 8. `hospitalized`: Cumulative number of hospitalized cases reported.
  51. 9. `hospitalizedCurr`: Number of actively hospitalized cases on that **day**.
  52. 10. `recovered`: Cumulative number of recovered cases reported.
  53. 11. `death`: Cumulative number of deaths reported.
  54. 12. `total_tested`: Cumulative number of tests conducted.
  55. 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.
  56. 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.
  57. ```{r}
  58. # Selecting the columns with cumulative numbers
  59. covid_df_all_states_daily <- covid_df_all_states %>%
  60. select(Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive)
  61. head(covid_df_all_states_daily)
  62. ```
  63. # Extracting the Top Ten countries in the number of tested cases
  64. ## Summarizing the data based on the `Country_Region` column.
  65. ```{r}
  66. covid_df_all_states_daily_sum <- covid_df_all_states_daily %>%
  67. group_by(Country_Region) %>%
  68. summarise(tested = sum(daily_tested),
  69. positive = sum(daily_positive),
  70. active = sum(active),
  71. hospitalized = sum(hospitalizedCurr)) %>%
  72. arrange(desc(tested)) #this is equivalent to `arrange(-tested)`
  73. covid_df_all_states_daily_sum
  74. #Date, Country_Region, active, hospitalizedCurr, daily_tested, daily_positive
  75. ```
  76. ## Taking the top 10
  77. ```{r}
  78. covid_top_10 <- head(covid_df_all_states_daily_sum, 10)
  79. covid_top_10
  80. ```
  81. # Identifying the Highest Positive Against Tested Cases
  82. ## Getting vectors
  83. ```{r}
  84. countries <- covid_top_10$Country_Region
  85. tested_cases <- covid_top_10$tested
  86. positive_cases <- covid_top_10$positive
  87. active_cases <- covid_top_10$active
  88. hospitalized_cases <- covid_top_10$hospitalized
  89. ```
  90. ## Naming vectors
  91. ```{r}
  92. names(positive_cases) <- countries
  93. names(tested_cases) <- countries
  94. names(active_cases) <- countries
  95. names(hospitalized_cases) <- countries
  96. ```
  97. ## Identifying
  98. ```{r}
  99. positive_cases
  100. sum(positive_cases)
  101. mean(positive_cases)
  102. positive_cases/sum(positive_cases)
  103. ```
  104. ```{r}
  105. positive_cases/tested_cases
  106. ```
  107. ## Conclusion
  108. ```{r}
  109. positive_tested_top_3 <- c("United Kingdom" = 0.11, "United States" = 0.10, "Turkey" = 0.08)
  110. ```
  111. # Keeping relevant information
  112. ```{r}
  113. # Creating vectors
  114. united_kingdom <- c(0.11, 1473672, 166909, 0, 0)
  115. united_states <- c(0.10, 17282363, 1877179, 0, 0)
  116. turkey <- c(0.08, 2031192, 163941, 2980960, 0)
  117. # Creating the matrix covid_mat
  118. covid_mat <- rbind(united_kingdom, united_states, turkey)
  119. # Naming columns
  120. colnames(covid_mat) <- c("Ratio", "tested", "positive", "active", "hospitalized")
  121. #d Displaying the matrix
  122. covid_mat
  123. ```
  124. # Putting all together
  125. ```{r}
  126. question <- "Which countries have had the highest number of positive cases against the number of tests?"
  127. answer <- c("Positive tested cases" = positive_tested_top_3)
  128. datasets <- list(
  129. original = covid_df,
  130. allstates = covid_df_all_states,
  131. daily = covid_df_all_states_daily,
  132. top_10 = covid_top_10
  133. )
  134. matrices <- list(covid_mat)
  135. vectors <- list(vector_cols, countries)
  136. data_structure_list <- list("dataframe" = datasets, "matrix" = matrices, "vector" = vectors)
  137. covid_analysis_list <- list(question, answer, data_structure_list)
  138. covid_analysis_list[[2]]
  139. ```