/R/ds_admin.ch.R

https://github.com/covid19datahub/COVID19 · R · 123 lines · 67 code · 12 blank · 44 comment · 3 complexity · e0b2769a8a9411aaaf0dd37948d64906 MD5 · raw file

  1. #' Federal Office of Public Health
  2. #'
  3. #' Data source for: Switzerland and Liechtenstein
  4. #'
  5. #' @param level 1, or 2 (only for Switzerland)
  6. #' @param state one of CH (Switzerland) or FL (Liechtenstein)
  7. #'
  8. #' @section Level 1:
  9. #' - confirmed cases
  10. #' - deaths
  11. #' - tests
  12. #' - total vaccine doses administered
  13. #' - people with at least one vaccine dose
  14. #' - people fully vaccinated
  15. #' - hospitalizations
  16. #' - intensive care
  17. #'
  18. #' @section Level 2:
  19. #' - confirmed cases
  20. #' - deaths
  21. #' - tests
  22. #' - total vaccine doses administered
  23. #' - people with at least one vaccine dose
  24. #' - people fully vaccinated
  25. #' - hospitalizations
  26. #' - intensive care
  27. #'
  28. #' @source https://www.covid19.admin.ch/en/overview
  29. #'
  30. #' @keywords internal
  31. #'
  32. admin.ch <- function(level, state = NULL) {
  33. if(state=="FL" & level!=1) return(NULL)
  34. if(state=="CH" & !level %in% 1:2) return(NULL)
  35. # metadata
  36. meta <- jsonlite::fromJSON("https://www.covid19.admin.ch/api/data/context")
  37. csv <- meta$sources$individual$csv
  38. # total vaccine doses
  39. x <- read.csv(csv$vaccDosesAdministered, na.strings = "NA")
  40. vaccines <- map_data(x, c(
  41. "date" = "date",
  42. "geoRegion" = "code",
  43. "sumTotal" = "vaccines"
  44. ))
  45. # people vaccinated
  46. x <- read.csv(csv$vaccPersonsV2, na.strings = "NA")
  47. vaccinated <- map_data(x, c(
  48. "date" = "date",
  49. "geoRegion" = "code",
  50. "sumTotal" = "total",
  51. "type" = "type",
  52. "age_group" = "age"
  53. ))
  54. # filter by total population and pivot
  55. vaccinated <- vaccinated %>%
  56. filter(age=="total_population") %>%
  57. pivot_wider(id_cols = c("code", "date"), names_from = "type", values_from = "total") %>%
  58. rename(people_vaccinated = COVID19AtLeastOneDosePersons,
  59. people_fully_vaccinated = COVID19FullyVaccPersons)
  60. # confirmed
  61. x <- read.csv(csv$daily$cases, na.strings = "NA")
  62. confirmed <- map_data(x, c(
  63. "datum" = "date",
  64. "geoRegion" = "code",
  65. "sumTotal" = "confirmed"
  66. ))
  67. # deaths
  68. x <- read.csv(csv$daily$death, na.strings = "NA")
  69. deaths <- map_data(x, c(
  70. "datum" = "date",
  71. "geoRegion" = "code",
  72. "sumTotal" = "deaths"
  73. ))
  74. # tests
  75. x <- read.csv(csv$daily$test, na.strings = "NA")
  76. tests <- map_data(x, c(
  77. "datum" = "date",
  78. "geoRegion" = "code",
  79. "sumTotal" = "tests"
  80. ))
  81. # hosp
  82. x <- read.csv(csv$daily$hospCapacity, na.strings = "NA")
  83. x <- x[x$type_variant=="nfp",]
  84. hosp <- map_data(x, c(
  85. "date" = "date",
  86. "geoRegion" = "code",
  87. "Total_Covid19Patients" = "hosp",
  88. "ICU_Covid19Patients" = "icu"
  89. ))
  90. # merge
  91. by <- c("code", "date")
  92. x <- confirmed %>%
  93. full_join(vaccines, by = by) %>%
  94. full_join(vaccinated, by = by) %>%
  95. full_join(deaths, by = by) %>%
  96. full_join(tests, by = by) %>%
  97. full_join(hosp, by = by)
  98. # clean code
  99. x <- x[!is.na(x$code),]
  100. # filter by state
  101. if(level==1){
  102. x <- x[x$code==state,]
  103. }
  104. # select only Swiss cantons
  105. else{
  106. x <- x[!(x$code %in% c("CH", "FL", "CHFL", "all", "neighboring_chfl", "unknown")),]
  107. }
  108. # convert date
  109. x$date <- as.Date(x$date)
  110. return(x)
  111. }