Download, read and format STATS19 data in one function.

get_stats19(year = NULL, type = "accidents", data_dir = tempdir(),
  file_name = NULL, format = TRUE, ask = TRUE)

Arguments

year

Single year for which file is to be downloaded.

type

One of 'Accidents', 'Casualties', 'Vehicles'; defaults to 'Accidents'. Or any variation of to search the file names with such as "acc" or "accid".

data_dir

Parent directory for all downloaded files. Defaults to tempdir().

file_name

The file name (DfT named) to download.

format

Switch to return raw read from file, default is TRUE.

ask

Should you be asked whether or not to download the files? TRUE by default.

Details

This function utilizes dl_stats19 and read_* functions and retuns a df. The file downloaded would be for a specific year (e.g 2017).

As this function uses dl_stats19 function, it can download many MB of data so ensure you have a sufficient disk space.

See also

Examples

get_stats19(year = 2017)
#> Files identified: dftRoadSafetyData_Accidents_2017.zip
#> http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/dftRoadSafetyData_Accidents_2017.zip
#> Data already exists in data_dir, not downloading
#> Data saved at /tmp/RtmpGajYm7/dftRoadSafetyData_Accidents_2017/Acc.csv
#> Reading in:
#> /tmp/RtmpGajYm7/dftRoadSafetyData_Accidents_2017/Acc.csv
#> # A tibble: 129,982 x 32 #> accident_index location_eastin… location_northi… longitude latitude #> <chr> <int> <int> <dbl> <dbl> #> 1 2017010001708 532920 196330 -0.0801 51.7 #> 2 2017010009342 526790 181970 -0.174 51.5 #> 3 2017010009344 535200 181260 -0.0530 51.5 #> 4 2017010009348 534340 193560 -0.0607 51.6 #> 5 2017010009350 533680 187820 -0.0724 51.6 #> 6 2017010009351 514510 172370 -0.354 51.4 #> 7 2017010009353 508640 181870 -0.435 51.5 #> 8 2017010009354 527880 181950 -0.158 51.5 #> 9 2017010009357 520940 192820 -0.254 51.6 #> 10 2017010009358 531430 178450 -0.108 51.5 #> # … with 129,972 more rows, and 27 more variables: police_force <chr>, #> # accident_severity <chr>, number_of_vehicles <int>, #> # number_of_casualties <int>, date <dttm>, day_of_week <chr>, time <chr>, #> # local_authority_district <chr>, local_authority_highway <chr>, #> # first_road_class <chr>, first_road_number <int>, road_type <chr>, #> # speed_limit <int>, junction_detail <chr>, junction_control <chr>, #> # second_road_class <chr>, second_road_number <int>, #> # pedestrian_crossing_human_control <chr>, #> # pedestrian_crossing_physical_facilities <chr>, light_conditions <chr>, #> # weather_conditions <chr>, road_surface_conditions <chr>, #> # special_conditions_at_site <chr>, carriageway_hazards <chr>, #> # urban_or_rural_area <chr>, #> # did_police_officer_attend_scene_of_accident <int>, #> # lsoa_of_accident_location <chr>
get_stats19(year = 2009)
#> Files identified: DfTRoadSafety_Accidents_2009.zip
#> http://data.dft.gov.uk.s3.amazonaws.com/road-accidents-safety-data/DfTRoadSafety_Accidents_2009.zip
#> Data already exists in data_dir, not downloading
#> Data saved at /tmp/RtmpGajYm7/DfTRoadSafety_Accidents_2009/DfTRoadSafety_Accidents_2009.csv
#> Reading in:
#> /tmp/RtmpGajYm7/DfTRoadSafety_Accidents_2009/DfTRoadSafety_Accidents_2009.csv
#> # A tibble: 163,554 x 32 #> accident_index location_eastin… location_northi… longitude latitude #> <chr> <int> <int> <dbl> <dbl> #> 1 200901BS70001 524910 180800 -0.201 51.5 #> 2 200901BS70002 525050 181040 -0.199 51.5 #> 3 200901BS70003 526490 177990 -0.180 51.5 #> 4 200901BS70004 524800 180300 -0.203 51.5 #> 5 200901BS70005 526930 177490 -0.173 51.5 #> 6 200901BS70006 526060 178730 -0.186 51.5 #> 7 200901BS70007 526580 177270 -0.179 51.5 #> 8 200901BS70008 526550 178580 -0.179 51.5 #> 9 200901BS70009 527310 179100 -0.167 51.5 #> 10 200901BS70010 526250 177370 -0.183 51.5 #> # … with 163,544 more rows, and 27 more variables: police_force <chr>, #> # accident_severity <chr>, number_of_vehicles <int>, #> # number_of_casualties <int>, date <dttm>, day_of_week <chr>, time <chr>, #> # local_authority_district <chr>, local_authority_highway <chr>, #> # first_road_class <chr>, first_road_number <int>, road_type <chr>, #> # speed_limit <int>, junction_detail <chr>, junction_control <chr>, #> # second_road_class <chr>, second_road_number <int>, #> # pedestrian_crossing_human_control <chr>, #> # pedestrian_crossing_physical_facilities <chr>, light_conditions <chr>, #> # weather_conditions <chr>, road_surface_conditions <chr>, #> # special_conditions_at_site <chr>, carriageway_hazards <chr>, #> # urban_or_rural_area <chr>, #> # did_police_officer_attend_scene_of_accident <int>, #> # lsoa_of_accident_location <chr>