| pair_ts | R Documentation |
This function adds observations from one time series to another time series using a matching process (e.g., nearest neighbour interpolation). This is useful when you have a main dataframe to which you need to add observations (e.g., those occurring closest in time) from another dataframe.
pair_ts( d1, d2, time_col, key_col = NULL, val_col, method = "match_ts_nearest", min_gap = NULL, max_gap = min_gap, units = "mins", control_beyond_gap = NULL )
d1 |
A dataframe that contains, at a minimum, a vector of time stamps, to which observations need to be added from |
d2 |
A dataframe that contains, at a minimum, a vector of time stamps and associated observations, to be added to |
time_col |
A character that defines the name of the column that contains time stamps in |
key_col |
(optional) A character that defines the name of the column that contains keys in |
val_col |
A character that defines the name of the column that contains observations in |
method |
A character that defines the matching method. The options currently implemented are |
min_gap |
(optional) A number that defines the minimum time gap (in user-defined units, see |
max_gap |
As above, for |
units |
A character that defines the units of the inputted |
control_beyond_gap |
A character that defines whether or not to set rows from |
The function returns a dataframe, d1, as inputted, with an added column (whose name is given by val_col), comprising values added from another dataframe, d2. Any observations in d1 for which there are not observations in d2 occurring within some time window (defined by min_gap and max_gap), if specified, are counted and, if requested, removed from the returned dataframe.
Edward Lavender
#### Example (1) Pair time series using method = "match_nearest_ts()"
# Define dataframe to which we want to add information
d1 <- data.frame(t = seq.POSIXt(as.POSIXct("2016-01-01"), as.POSIXct("2016-01-02"), by = "hours"))
# Define dataframe in which information is contained
d2 <- data.frame(t = seq.POSIXt(as.POSIXct("2016-01-01"), as.POSIXct("2016-01-02"), by = "mins"))
d2$vals <- runif(nrow(d2), 0, 50)
pair_ts(d1, d2, time_col = "t", val_col = "vals", method = "match_ts_nearest")
#### Example (2) Pair time series sing method = "match_nearest_ts_by_key()"
# Define dataframes
d1 <- data.frame(t = as.POSIXct(c("2016-01-01 18:00:00",
"2016-01-01 17:00:00",
"2016-01-01 13:00:00",
"2016-01-01 14:00:00",
"2016-01-01 17:00:00",
"2016-01-01 21:00:00")),
key = c(2, 2, 2, 1, 1, 3))
d2 <- data.frame(t = as.POSIXct(c("2016-01-01 21:00:00",
"2016-01-01 14:00:00",
"2016-01-01 18:00:00",
"2016-01-01 17:00:00",
"2016-01-01 22:00:00",
"2016-01-01 20:00:00",
"2016-01-01 13:00:00",
"2016-01-01 17:00:00",
"2016-01-01 16:00:00")),
key = c(2, 2, 2, 2, 2, 3, 3, 1, 1),
vals = stats::runif(9, 0, 1))
pair_ts(d1, d2,
time_col = "t", key_col = "key", val_col = "vals",
method = "match_ts_nearest_by_key")
#### Example (3) Flag observations that exceed a min/max gap
pair_ts(d1, d2,
time_col = "t", key_col = "key", val_col = "vals",
method = "match_ts_nearest_by_key",
min_gap = 0,
max_gap = 1,
control_beyond_gap = "remove")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.