collapse_ranges {neatRanges} | R Documentation |
Collapses the consecutive date or timestamp ranges into one record.
Description
The date/time ranges where the gap between two records is equal to or less than max_gap parameter are collapsed into one record.
Usage
collapse_ranges(
df,
groups = NULL,
start_var = NULL,
end_var = NULL,
startAttr = NULL,
endAttr = NULL,
dimension = c("date", "timestamp"),
max_gap = 0L,
fmt = "%Y-%m-%d",
tz = "UTC",
origin = "1970-01-01"
)
Arguments
df |
Your data frame (object of class 'data.frame' or 'data.table') |
groups |
Grouping variables, character strings |
start_var |
Start of the range, character of length 1L |
end_var |
End of the range, character of length 1L |
startAttr |
Attributes linked to start of the range which should be kept (converted to character type by default) |
endAttr |
Attributes linked to end of the range which should be kept (converted to character type by default) |
dimension |
Indicate whether your range includes only dates ('date') or also timestamp ('timestamp'). Defaults to 'date' |
max_gap |
Gap between date or timestamp ranges, e.g. for 0, default, it will put together all records where there is no gap in-between |
fmt |
The format of your date or timestamp field, defaults to YMD |
tz |
Time zone, defaults to UTC |
origin |
Origin for timestamp conversion, defaults to '1970-01-01' |
Value
'data.frame' if initial input is a 'data.frame', 'data.table' if original object is a 'data.table' with collapsed records.
Examples
df_collapse <- data.frame(
id = c(rep("1111", 3), rep("2222", 3)),
rating = c("A+", "AA", "AA", rep("B-", 3)),
start_date = c(
"2014-01-01", "2015-01-01", "2016-01-01",
"2017-01-01", "2018-01-01", "2019-01-01"
),
end_date = c(
"2014-12-31", "2015-12-31", "2016-03-01",
"2017-01-31", "2018-12-31", "2020-02-01"
)
)
collapse_ranges(df_collapse, c("id", "rating"), "start_date", "end_date")