Create a combined mts from any number of mts objects or from a list of mts objects. The resulting mts object with contain all deviceDeploymentIDs found in any incoming mts and will have a regular time axis covering the the entire range of incoming data.

If incoming time ranges are non-contiguous, the resulting mts will have gaps filled with NA values.

An error is generated if the incoming mts objects have non-identical metadata for the same deviceDeploymentID unless replaceMeta = TRUE.

mts_combine(
  ...,
  replaceMeta = FALSE,
  overlapStrategy = c("replace all", "replace na")
)

Arguments

...

Any number of valid mts objects.

replaceMeta

Logical specifying whether to allow replacement of metadata associated with deviceDeploymentIDs.

overlapStrategy

Strategy to use when data found in time series overlaps.

Value

An mts time series object containing all time series found in the incoming mts objects. (A list with meta and data dataframes.)

Note

Data for any deviceDeploymentIDs shared among mts objects are combined with a "later is better" sensibility where any data overlaps exist. To handle this, incoming mts objects are first split into "shared" and "unshared" parts.

Any "shared" parts are ordered based on the time stamp of their last record. Then dplyr::distinct() is used to remove records with duplicate datetime fields.

With overlapStrategy = "replace all", any data records found in "later" mts objects are preferentially retained before the "shared" data are finally reordered by ascending datetime.

With overlapStrategy = "replace missing", only missing values in "earlier" mts objects are replaced with data records from "later" time series.

The final step is combining the "shared" and "unshared" parts and placing them on a uniform time axis.

Examples

library(MazamaTimeSeries)

ids1 <- example_mts$meta$deviceDeploymentID[1:5]
ids2 <- example_mts$meta$deviceDeploymentID[4:6]
ids3 <- example_mts$meta$deviceDeploymentID[8:10]

mts1 <-
  example_mts %>%
  mts_filterMeta(deviceDeploymentID %in% ids1) %>%
  mts_filterDate(20190701, 20190703)

mts2 <-
  example_mts %>%
  mts_filterMeta(deviceDeploymentID %in% ids2) %>%
  mts_filterDate(20190704, 20190706)

mts3 <-
  example_mts %>%
  mts_filterMeta(deviceDeploymentID %in% ids3) %>%
  mts_filterDate(20190705, 20190708)

mts <- mts_combine(mts1, mts2, mts3)

# Should have 1:6 + 8:10 = 9 meta records and the full date range
nrow(mts$meta)
#> [1] 9
range(mts$data$datetime)
#> [1] "2019-07-01 07:00:00 UTC" "2019-07-08 06:00:00 UTC"