Skip to contents

round_dates() rounds movement dates in a movenet-format movement tibble, down to the first day of the specified time unit. It optionally summarises movement data with the same origin and destination by this time unit. By default, this involves summation of weights, but alternative or additional summary functions can be provided through ....

Usage

round_dates(
  data,
  unit,
  week_start = getOption("lubridate.week.start", 7),
  sum_weight = TRUE,
  ...
)

Arguments

data

A movenet-format movement tibble.

unit

A character string specifying a time unit or a multiple of a unit for movement dates to be rounded down to. Valid base units are day, week, month, bimonth, quarter, season, halfyear and year. Arbitrary unique English abbreviations as in the lubridate::period() constructor are allowed. Rounding to multiples of units (except weeks) is supported.

week_start

Week start day, only relevant for rounding down dates and/ or aggregating data by week. Default is the value of the lubridate.week.start option, or 7 (Sunday) if this option is not set. Full or abbreviated names of the days of the week can be in English or as provided by the current locale.

sum_weight

If this is TRUE (the default), weights are summed over unit, for all rows with the same origin and destination. The name of the weight column will remain the same.

...

data-masking Additional or alternative summary function(s), of the form name = value, to pass on to dplyr::summarise(). Any summary functions will be applied to data, grouped by origin, destination, and rounded-down date. The specified name will be the column name in the resulting tibble. The value can be:

  • A vector of length 1, e.g. min(x), n(), or sum(is.na(y)).

  • A vector of length n, e.g. quantile().

  • A tibble, to add multiple columns from a single expression.

Value

A movement tibble like data, but with rounded-down movement dates.

If sum_weight is TRUE or any summary functions are provided through ..., the returned tibble contains data summarised by origin, destination, and unit, and may thus have a decreased length compared to data.

Columns for which a summary function is not provided, are dropped from the resulting tibble.

Details

Requires that an appropriate movement config file is loaded, to correctly identify origin (from), destination (to), date and weight columns in data.

Movement dates are rounded to the first day of the rounding unit specified in unit.

If sum_weight is TRUE, weights are summed over all rows in data with the same origin, destination, and rounded-down date. The summed weight column is temporarily renamed, so that any additional weight-dependent summary functions passed through ... use the original weights rather than the sums.

If any summary functions are provided through ..., the specified data are summarised accordingly, over all rows in data with the same origin, destination, and rounded-down date. Be careful when using' existing names: the corresponding columns will be immediately updated with the new data and this can affect subsequent operations referring to this name.

Columns for which a summary function is not provided, are dropped from the resulting tibble.

See also

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/AppData/Local/Temp/Rtmp25xhub/temp_libpath11484970381d/movenet/configurations/ScotEID.yml

# Round dates down to the first day of the month, aggregate by summing up
# weights. This silently drops the optional movement_reference column.
monthly_data <- round_dates(example_movement_data, "month")
head(monthly_data)
#> # A tibble: 6 x 4
#>   departure_cph dest_cph    departure_date qty_pigs
#>   <chr>         <chr>       <date>            <dbl>
#> 1 10/161/7572   17/138/4106 2019-02-01          169
#> 2 10/161/7572   26/750/9771 2019-05-01          113
#> 3 10/161/7572   27/468/2072 2019-05-01           59
#> 4 10/161/7572   31/473/4857 2019-05-01           86
#> 5 10/161/7572   45/864/8651 2019-08-01           72
#> 6 10/161/7572   47/232/6124 2019-11-01          148

# Round dates down to the first day of the month, and aggregate by summing
# up weights and listing movement reference numbers together
monthly_data_w_refs <- round_dates(example_movement_data, "month",
                                   movement_refs = list(movement_reference))
head(monthly_data_w_refs)
#> # A tibble: 6 x 5
#>   departure_cph dest_cph    departure_date qty_pigs movement_refs
#>   <chr>         <chr>       <date>            <dbl> <list>       
#> 1 10/161/7572   17/138/4106 2019-02-01          169 <dbl [1]>    
#> 2 10/161/7572   26/750/9771 2019-05-01          113 <dbl [1]>    
#> 3 10/161/7572   27/468/2072 2019-05-01           59 <dbl [1]>    
#> 4 10/161/7572   31/473/4857 2019-05-01           86 <dbl [1]>    
#> 5 10/161/7572   45/864/8651 2019-08-01           72 <dbl [1]>    
#> 6 10/161/7572   47/232/6124 2019-11-01          148 <dbl [1]>    


# Round dates down to the first day of the week (Monday) without aggregating
weekly_data <- round_dates(example_movement_data,
                           unit = "week",
                           week_start = "Monday",
                           sum_weight = FALSE)
head(weekly_data)
#> # A tibble: 6 x 5
#>   departure_cph dest_cph    departure_date qty_pigs movement_reference
#>   <chr>         <chr>       <date>            <dbl>              <dbl>
#> 1 95/216/1100   19/818/9098 2019-02-04           97             304781
#> 2 69/196/5890   71/939/3228 2019-08-12          167             229759
#> 3 52/577/5349   82/501/8178 2019-09-09          115              36413
#> 4 39/103/5541   13/282/1763 2019-10-21          125             488616
#> 5 41/788/6464   57/418/6011 2019-10-14          109             581785
#> 6 69/393/9398   39/947/2201 2019-09-30           72             564911

# Clean-up: Reinstate previous configurations
movenetenv$options <- old_config
rm("old_config", "movenetenv", "monthly_data", "monthly_data_w_refs",
  "weekly_data")