Skip to content

Commit

Permalink
Attempt addressing #235
Browse files Browse the repository at this point in the history
  • Loading branch information
layik committed Jul 27, 2024
1 parent 6b74be0 commit 0c9a0d9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
28 changes: 26 additions & 2 deletions R/read.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions data-raw/stats19_variables.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Binary file modified data/stats19_variables.rda
Binary file not shown.

0 comments on commit 0c9a0d9

Please sign in to comment.