Allow count to work on multiple columns at once
Closed this issue · 1 comments
TimTaylor commented
Count should be made to work on multiple columns at once.
TimTaylor commented
Experimenting in https://github.com/reconhub/incidence2/tree/counting branch. @thibautjombart New behaviour
library(incidence2)
library(magrittr)
url <- "https://api.coronavirus.data.gov.uk/v2/data?areaType=nation&metric=newAdmissions&metric=newCasesBySpecimenDate&format=csv"
dat <- read.csv(url)
res <-
dat %>%
incidence(
date_index = date,
groups = areaName,
counts = c(newCasesBySpecimenDate, newAdmissions)
)
# res now has two count columns
res
#> An incidence2 object: 1,475 x 4
#> 4071183 newCasesBySpecimenDate from days 2020-01-30 to 2021-02-16
#> 427866 newAdmissions from days 2020-01-30 to 2021-02-16
#> interval: 1 day
#> cumulative: FALSE
#>
#> date_index areaName newCasesBySpecimenDate newAdmissions
#> <date> <chr> <int> <int>
#> 1 2020-01-30 England 2 0
#> 2 2020-01-31 England 0 0
#> 3 2020-02-01 England 0 0
#> 4 2020-02-02 England 0 0
#> 5 2020-02-03 England 0 0
#> 6 2020-02-03 Northern Ireland 0 0
#> 7 2020-02-04 England 0 0
#> 8 2020-02-04 Northern Ireland 0 0
#> 9 2020-02-05 England 1 0
#> 10 2020-02-05 Northern Ireland 0 0
#> # … with 1,465 more rows
get_count_names(res)
#> [1] "newCasesBySpecimenDate" "newAdmissions"
# subsetting columns maintains incidence class as long as atleast one count
# column remains present
res[-4] # maintains class
#> An incidence2 object: 1,475 x 3
#> 4071183 newCasesBySpecimenDate from days 2020-01-30 to 2021-02-16
#> interval: 1 day
#> cumulative: FALSE
#>
#> date_index areaName newCasesBySpecimenDate
#> <date> <chr> <int>
#> 1 2020-01-30 England 2
#> 2 2020-01-31 England 0
#> 3 2020-02-01 England 0
#> 4 2020-02-02 England 0
#> 5 2020-02-03 England 0
#> 6 2020-02-03 Northern Ireland 0
#> 7 2020-02-04 England 0
#> 8 2020-02-04 Northern Ireland 0
#> 9 2020-02-05 England 1
#> 10 2020-02-05 Northern Ireland 0
#> # … with 1,465 more rows
res[-3] # maintains class
#> An incidence2 object: 1,475 x 3
#> 427866 newAdmissions from days 2020-01-30 to 2021-02-16
#> interval: 1 day
#> cumulative: FALSE
#>
#> date_index areaName newAdmissions
#> <date> <chr> <int>
#> 1 2020-01-30 England 0
#> 2 2020-01-31 England 0
#> 3 2020-02-01 England 0
#> 4 2020-02-02 England 0
#> 5 2020-02-03 England 0
#> 6 2020-02-03 Northern Ireland 0
#> 7 2020-02-04 England 0
#> 8 2020-02-04 Northern Ireland 0
#> 9 2020-02-05 England 0
#> 10 2020-02-05 Northern Ireland 0
#> # … with 1,465 more rows
res[1:2] # drops class
#> # A tibble: 1,475 x 2
#> date_index areaName
#> <date> <chr>
#> 1 2020-01-30 England
#> 2 2020-01-31 England
#> 3 2020-02-01 England
#> 4 2020-02-02 England
#> 5 2020-02-03 England
#> 6 2020-02-03 Northern Ireland
#> 7 2020-02-04 England
#> 8 2020-02-04 Northern Ireland
#> 9 2020-02-05 England
#> 10 2020-02-05 Northern Ireland
#> # … with 1,465 more rows
# plot will default to first value in get_count_names(res)
res %>% plot()
res %>% plot(count = "newAdmissions")
Created on 2021-02-18 by the reprex package (v1.0.0)