ropensci/stats19

Accidents from multiple years extracted when using a year pre-2009

ccb2n19 opened this issue · 3 comments

Just spotted one issue while extracting accidents across multiple years using get_stats19.

Using a year pre-2009 returns cases from 2005 to 2014. Whereas using 2009 or later returns just cases from that year. I couldn't see this mentioned in other issues, so thought I'd flag it.

A reproducible example is below.

Set some years and LAs to get data for:

library(tidyverse)
library(stats19)
library(lubridate)
library(sf)

years <- tibble(year_map = c(2007:2009))

la_vec <- c("Coventry", "Birmingham")

Create a function to import the data for each year and filter it for specific local authorities:

get_accidents <- function(x, las)
{ 
  x %>% 
    get_stats19(
      type = "accidents"
    ) %>%
    filter(
      local_authority_district %in% las
    )
}

Apply the function to the tibble of years:

accidents <- years %>%
  mutate(
    data = map(
      year_map
      ,get_accidents
      ,las = la_vec
    )
  )

The following shows the issue with the result:

accidents %>%
  unnest() %>%
  mutate(year = year(date)) %>%
  group_by(year_map) %>%
  summarise(max_year = max(year),
         min_year = min(year))

# A tibble: 3 x 3
  year_map max_year min_year
     <int>    <dbl>    <dbl>
1     2007     2014     2005
2     2008     2014     2005
3     2009     2009     2009

It's quite an easy fix by adapting adding a second filter to the function, but maybe worth fixing if this is an actual issue? I had a quick scan of the files in the repository but I couldn't find where a fix would be implemented (I'm pretty new to this). I'd be happy to contribute if someone could point me in the right direction.

Thanks to everyone for the package, too.

(OS is Windows 10 Home. R version 4.0.3)

Hi @ccb2n19 good point, it should be documented. Proposed fix here: #189. Any comments, suggestions and contributions very welcome, thanks for supporting development of this package.

Looks good to me. Thanks. I'll post again if other comments or ideas come up.

Great, closing for now but if any other issues arise let us know. Comments and suggested changes welcome!