Skip to contents

Utility function to use alongside the mapping object creator functions such as mapping_entry_points, mapping_animal_mobility, etc.. To mapping column names to the required values and validate the dataset for use in riskintro analysis workflows.

Usage

apply_mapping(dataset, mapping, validate = TRUE)

Arguments

dataset

dataset to apply mapping to

mapping

mapping to apply to dataset

validate

whether to validate the dataset, TRUE by default

Value

dataset with renamed columns based on mapping and with attribute "table_name" set to the expected table type and attribute "valid" indicating if validated or not.

See also

Examples


entry_points_fp <-
    system.file(
      package = "riskintrodata",
      "samples",
      "tunisia",
      "entry_points", "BORDER_CROSSING_POINTS.csv"
    )
entry_points <- readr::read_csv(entry_points_fp)
#> Rows: 110 Columns: 6
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (4): NAME, TYPE, MODE, SOURCES
#> dbl (2): LONGITUDE_X, LATITUDE_Y
#> 
#>  Use `spec()` to retrieve the full column specification for this data.
#>  Specify the column types or set `show_col_types = FALSE` to quiet this message.


entry_points_mapped <- apply_mapping(
  dataset = entry_points,
  mapping = mapping_entry_points(
    point_name = "NAME",
    lng = "LONGITUDE_X",
    lat = "LATITUDE_Y",
    mode = "MODE",
    type = "TYPE",
    sources = "SOURCES"
  ),
  validate = TRUE
)
#>  All data in "entry_points" valided.

if(FALSE){
  entry_points_wrong <- entry_points
  entry_points_wrong$TYPE <- rep_len(c("PIF Maritime", "Passage contrebande", "'ELLO"),
                                     length.out = nrow(entry_points))
  entry_points_wrong$LONGITUDE_X <- rep_len(c(10.00, 15.00, NA_real_),
                                            length.out = nrow(entry_points))
  entry_points_wrong[23:30, "NAME"] <- NA_character_

  entry_points_wrong$NAME <- NULL


  entry_points_mapped <- apply_mapping(
    dataset = entry_points_wrong,
    mapping = mapping_entry_points(
      point_name = "NAME",
      lng = "LONGITUDE_X",
      lat = "LATITUDE_Y",
      mode = "MODE",
      type = "TYPE",
      sources = "SOURCES"
    ),
    validate = TRUE
  )
}