diff --git a/R/read.R b/R/read.R index 7bdfe3b..de8d2f0 100644 --- a/R/read.R +++ b/R/read.R @@ -47,7 +47,7 @@ read_collisions = function(year = NULL, } # read the data in suppressWarnings({ - ac = readr::read_csv(path) + ac = readr::read_csv(path, col_types = readr::cols(!!!col_types)) }) if(format) @@ -169,5 +169,29 @@ read_ve_ca = function(path) { read_null = function(path, ...) { if(is.null(path)) return(NULL) - readr::read_csv(path, ...) + readr::read_csv(path, col_types = readr::cols(!!!col_types), ...) } + +# possibly in utils +# Convert the 'type' column to readr's col_type format +convert_to_col_type <- function(type) { + switch(type, + character = readr::col_character(), + numeric = readr::col_double(), + integer = readr::col_integer(), + logical = readr::col_logical(), + date = readr::col_date(), + datetime = readr::col_datetime(), + readr::col_guess()) +} + +data("stats19_variables", package = "stats19") + +# Create a named list of column types +unique_vars = unique(stats19_variables$variable) +unique_types = sapply(unique_vars, function(v) { + type = stats19_variables$type[stats19_variables$variable == v][1] + convert_to_col_type(type) +}) + +col_types = setNames(unique_types, unique_vars) diff --git a/data-raw/stats19_variables.csv b/data-raw/stats19_variables.csv index 1070cfc..6daa074 100644 --- a/data-raw/stats19_variables.csv +++ b/data-raw/stats19_variables.csv @@ -43,7 +43,7 @@ Casualty,accident_index,unique value for each accident. The accident_index combi Casualty,accident_reference,"In year id used by the police to reference a collision. It is not unique outside of the year, use accident_index for linking to other years",accident_reference,character Casualty,accident_year,NA,accident_year,numeric Casualty,age_band_of_casualty,NA,age_band_of_casualty,character -Casualty,age_of_casualty,NA,age_of_casualty,character +Casualty,age_of_casualty,NA,age_of_casualty,numeric Casualty,bus_or_coach_passenger,NA,bus_or_coach_passenger,character Casualty,car_passenger,NA,car_passenger,character Casualty,casualty_class,NA,casualty_class,character @@ -73,8 +73,8 @@ Vehicle,accident_index,unique value for each accident. The accident_index combin Vehicle,accident_reference,"In year id used by the police to reference a collision. It is not unique outside of the year, use accident_index for linking to other years",accident_reference,character Vehicle,accident_year,NA,accident_year,numeric Vehicle,age_band_of_driver,NA,age_band_of_driver,character -Vehicle,age_of_driver,NA,age_of_driver,character -Vehicle,age_of_vehicle,NA,age_of_vehicle,character +Vehicle,age_of_driver,NA,age_of_driver,numeric +Vehicle,age_of_vehicle,NA,age_of_vehicle,numeric Vehicle,driver_home_area_type,field introduced in 1999,driver_home_area_type,character Vehicle,driver_imd_decile,field introduced in 2016,driver_imd_decile,character Vehicle,engine_capacity_cc,NA,engine_capacity_cc,character diff --git a/data/stats19_variables.rda b/data/stats19_variables.rda index ac2ff5c..c6170dc 100644 Binary files a/data/stats19_variables.rda and b/data/stats19_variables.rda differ