/R/ds_github.cssegisanddata.covid19unified.R

https://github.com/covid19datahub/COVID19 · R · 82 lines · 29 code · 8 blank · 45 comment · 2 complexity · d8528f8eef376d834a37d5b3554648c6 MD5 · raw file

  1. #' Johns Hopkins Center for Systems Science and Engineering
  2. #'
  3. #' Data source for: Worldwide
  4. #'
  5. #' @param level 1, 2, 3
  6. #' @param iso the 2-letter (level=1) or 3-letter (level>1) ISO code of the country
  7. #'
  8. #' @section Level 1:
  9. #' - confirmed cases
  10. #' - deaths
  11. #' - recovered
  12. #' - tests
  13. #' - hospitalizations
  14. #' - intensive care
  15. #' - patients requiring ventilation
  16. #'
  17. #' @section Level 2:
  18. #' - confirmed cases
  19. #' - deaths
  20. #' - recovered
  21. #' - tests
  22. #' - hospitalizations
  23. #' - intensive care
  24. #' - patients requiring ventilation
  25. #'
  26. #' @section Level 3:
  27. #' - confirmed cases
  28. #' - deaths
  29. #' - recovered
  30. #' - tests
  31. #' - hospitalizations
  32. #' - intensive care
  33. #' - patients requiring ventilation
  34. #'
  35. #' @source https://github.com/CSSEGISandData/COVID-19_Unified-Dataset
  36. #'
  37. #' @keywords internal
  38. #'
  39. github.cssegisanddata.covid19unified <- function(level, iso){
  40. if(!level %in% 1:3) return(NULL)
  41. # JHU ids
  42. ids <- iso
  43. if(level!=1){
  44. db <- extdata(sprintf("db/%s.csv", iso))
  45. ids <- db$id_github.cssegisanddata.covid19unified[db$administrative_area_level==level]
  46. }
  47. # download
  48. url <- "https://github.com/CSSEGISandData/COVID-19_Unified-Dataset/blob/master/COVID-19.rds?raw=true"
  49. file <- tempfile()
  50. download.file(url, file, mode = "wb", quiet = TRUE)
  51. x <- readRDS(file)
  52. # filter
  53. x <- x[which((x$ID %in% ids) & x$Age=="Total" & x$Sex=="Total" & x$Cases>0),]
  54. # select data source with best data coverage
  55. s <- names(which.max(table(x$Source)))
  56. x <- x[x$Source==s,]
  57. # pivot
  58. x <- tidyr::pivot_wider(x, id_cols = c("ID", "Date"), names_from = "Type", values_from = "Cases")
  59. # map values
  60. x <- map_data(x, c(
  61. "ID" = "id",
  62. "Date" = "date",
  63. "Tests" = "tests",
  64. "Recovered" = "recovered",
  65. "Confirmed" = "confirmed",
  66. "Deaths" = "deaths",
  67. "Hospitalized_Now" = "hosp",
  68. "ICU_Now" = "icu",
  69. "Ventilator_Now" = "vent"
  70. ))
  71. # date
  72. x$date <- as.Date(x$date)
  73. return(x)
  74. }