Skip to content

Instantly share code, notes, and snippets.

@layik
Last active October 22, 2020 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save layik/72f9970c631d9e2ccf6811303a0da1de to your computer and use it in GitHub Desktop.
Save layik/72f9970c631d9e2ccf6811303a0da1de to your computer and use it in GitHub Desktop.
library(stats19)
n = c("caNotINac","veNotINac","acNotINca", "acNotINve")
check = function(year = 2019) {
if(is.null(year) || is.na(year)) stop("Year is required")
ac = get_stats19(year = year, type = "ac")
ca = get_stats19(year = year, type = "ca")
ve = get_stats19(year = year, type = "ve")
# all(ca$accident_index %in% ac$accident_index)
# all(ve$accident_index %in% ac$accident_index)
caNotINac = which(!ca$accident_index %in% ac$accident_index)
veNotINac = which(!ve$accident_index %in% ac$accident_index)
acNotINca = which(!ac$accident_index %in% ca$accident_index)
acNotINve = which(!ac$accident_index %in% ve$accident_index)
v = list(caNotINac, veNotINac, acNotINca, acNotINve)
names(v) = n
return(v)
}
# more years means more time
years = 2015:2019
checks = lapply(years, function(x) check(x))
l = lapply(checks, function(x) list(
caNotINac = length(x[[1]]),
veNotINac = length(x[[2]]),
acNotINca = length(x[[3]]),
acNotINve = length(x[[4]])
))
d = as.data.frame(matrix(unlist(l), nrow=length(unlist(l[1]))))
names(d) = years
rownames(d) = n
d
@Robinlovelace
Copy link

Heads-up, I can reproduce this, I get:

#>           2015 2016 2017 2018 2019
#> caNotINac   37   11   25   76   42
#> veNotINac   53   12   36   96   54
#> acNotINca    0    0    0    0    0
#> acNotINve    0    0    0    0    0

@layik
Copy link
Author

layik commented Oct 22, 2020

Thanks @Robinlovelace for spotting the sf issue, now we can be sure the data is consistent:

library(stats19)
#> Data provided under OGL v3.0. Cite the source and link to:
#> www.nationalarchives.gov.uk/doc/open-government-licence/version/3/
n = c("caNotINac","veNotINac","acNotINca", "acNotINve")
check = function(year = 2019) {
  if(is.null(year) || is.na(year)) stop("Year is required")
  ac = get_stats19(year = year, type = "ac")
  ca = get_stats19(year = year, type = "ca")
  ve = get_stats19(year = year, type = "ve")

  # all(ca$accident_index %in% ac$accident_index)
  # all(ve$accident_index %in% ac$accident_index)

  caNotINac = which(!ca$accident_index %in% ac$accident_index)
  veNotINac = which(!ve$accident_index %in% ac$accident_index)
  acNotINca = which(!ac$accident_index %in% ca$accident_index)
  acNotINve = which(!ac$accident_index %in% ve$accident_index)
  v = list(caNotINac, veNotINac, acNotINca, acNotINve)
  names(v) = n
  return(v)
}
# more years means more time
years = 2015:2019
checks = lapply(years, function(x) check(x))
#> Files identified: RoadSafetyData_Accidents_2015.zip
#>    http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/RoadSafetyData_Accidents_2015.zip
#....
#> Data saved at /tmp/Rtmp3KyY23/DfTRoadSafety_Vehicles_2019/Road Safety Data- Vehicles 2019.csv
l = lapply(checks, function(x) list(
  caNotINac = length(x[[1]]),
  veNotINac = length(x[[2]]),
  acNotINca = length(x[[3]]),
  acNotINve = length(x[[4]])
))
d = as.data.frame(matrix(unlist(l), nrow=length(unlist(l[1]))))
names(d) = years
rownames(d) = n
d
#>           2015 2016 2017 2018 2019
#> caNotINac    0    0    0    0    0
#> veNotINac    0    0    0    0    0
#> acNotINca    0    0    0    0    0
#> acNotINve    0    0    0    0    0

Created on 2020-10-22 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment