Skip to contents

reformat_data() reads in movement or holding data from data (either a data frame or a delimited data file) and reshapes them to a format compatible with movenet pseudonymisation, network analysis, and modelling workflows.

N.B.: To correctly interpret and process data, the function requires configurations of the relevant type to be loaded into the movenet environment.

reformat_data() processes data as follows:

  • Columns with minimally required data, and any additional optional columns as indicated in the loaded configurations, are extracted from data.

  • Column headers are converted to unique ASCII-compliant and syntactically valid names.

  • Data formats for date, weight, coord_x, coord_y, and/or herd_size columns are checked.

  • For movement data files (type == "movement"): Dates in the date column are converted to date format.

  • For holding data files (type == "holding"): If coord_x and coord_y columns are present, these are converted to a single simple feature (sf) list-column named "coordinates". Coordinates are converted from the crs specified in the config file to ETRS89 (EPSG:4258).

Usage

reformat_data(data, type)

Arguments

data

Either a path to a delimited file with movement or holding data, or a data frame holding such data.

type

Character string representing the type of data contained in data: either "movement" or "holding".

Value

A tibble with (a subset of) columns from data, reordered and reformatted according to movenet format requirements.

For movement data (type == "movement"), columns will include:

  • from (character format).

  • to (character format).

  • date (date format).

  • weight (double format).

  • Any optional columns as indicated by the loaded movedata_cols configurations (formats as determined by readr::type_convert()).

For holding data (type == "holding"), columns will include:

  • id (character format).

  • Any optional columns as indicated by the loaded holdingdata_cols configurations (formats as determined by readr::type_convert()).

  • If the loaded configurations include coord_x and coord_y, the returned tibble will instead include a single column named "coordinates" (sf list-column, class sfc_POINT).

Details

If the movenet environment contains movedata_cols or holdingdata_cols configurations stored as column indices (rather than column headers), calling reformat_data() replaces these configuration values with the appropriate column headers. A warning message is generated to indicate any such configuration changes.

See also

Other functions for initial data processing: asciify(), load_config()

Examples

# Set-up: Save movenet environment with current configurations
movenetenv <- movenet:::movenetenv
old_config <- movenetenv$options

# Load a movement config file
load_config(system.file("configurations", "ScotEID.yml",
                        package = "movenet"))
#> Successfully loaded config file: C:/Users/cboga/OneDrive - University of Glasgow/Documents/R/win-library/4.1/movenet/configurations/ScotEID.yml

# Read in and reformat a movement data file
movement_data <-
  reformat_data(system.file("extdata", "example_movement_data.csv",
                            package = "movenet"),
                type = "movement")
head(movement_data)
#> # A tibble: 6 x 4
#>   departure_cph dest_cph    departure_date qty_pigs
#>   <chr>         <chr>       <date>            <dbl>
#> 1 95/216/1100   19/818/9098 2019-02-08           97
#> 2 69/196/5890   71/939/3228 2019-08-15          167
#> 3 52/577/5349   82/501/8178 2019-09-15          115
#> 4 39/103/5541   13/282/1763 2019-10-26          125
#> 5 41/788/6464   57/418/6011 2019-10-17          109
#> 6 69/393/9398   39/947/2201 2019-10-06           72

# Load a holding config file
load_config(system.file("configurations", "fakeScotEID_holding.yml",
                        package = "movenet"))
#> Successfully loaded config file: C:/Users/cboga/OneDrive - University of Glasgow/Documents/R/win-library/4.1/movenet/configurations/fakeScotEID_holding.yml

# Read in and reformat a holding data file
holding_data <-
  reformat_data(system.file("extdata", "example_holding_data.csv",
                           package = "movenet"),
                type = "holding")
head(holding_data)
#> # A tibble: 6 x 4
#>   cph         holding_type herd_size           coordinates
#>   <chr>       <chr>            <dbl>           <POINT [°]>
#> 1 68/575/1991 GXFSR             2111   (3.718568 52.69096)
#> 2 51/469/9863 SCHZQ             2134  (-4.959035 51.88195)
#> 3 32/532/8560 HEJDE             2140   (5.709143 51.97547)
#> 4 82/501/8178 IQALL             2141  (-2.983365 57.55851)
#> 5 29/675/4499 YUFUC             2148   (5.586477 50.50902)
#> 6 59/516/9442 IATKP             2151 (-0.3323066 58.84295)

# Clean-up: Reinstate previous configurations
movenetenv$options <- old_config
rm("old_config", "movenetenv", "movement_data", "holding_data")