aggregate_index: Aggregate the index of a 'tsibble'

View source: R/aggregate_index.R

aggregate_indexR Documentation

Aggregate the index of a tsibble

Description

[Experimental]

aggregate_index() allows you to aggregate the index of a tsibble object by applying a specific function to its measured variables.

Usage

aggregate_index(data, unit, fun = NULL, week_start = 1)

Arguments

data

A tsibble object.

unit

A string indicating at which time unit the index must be aggregated. Valid values are: ⁠“seconds”⁠, ⁠“minutes”⁠, ⁠“hours”⁠, ⁠“days”⁠, ⁠“weeks”⁠, ⁠“months”⁠, ⁠“quarters”⁠, and ⁠“years”⁠) (default: "minutes").

fun

(optional) The function to be applied to each measure variable of data. If NULL, aggregate_index() will apply its default function (see the Details section to learn more) (default: NULL).

week_start

(optional) an integer number indicating the day on which the week starts (1 for Monday and 7 for Sunday). This is only used when ⁠unit == "weeks⁠ (default: 1).

Details

aggregate_index() was created to easily regularize tsibble objects. If you need more control while doing this operation, check the index_by() function provided by the tsibble package.

Default function

If fun == NULL, aggregate_index() will use the following function to transform each measured variable:

function(x) {
    checkmate::assert_atomic_vector(x)

    if (is.numeric(x) && !all(nchar(x) == 1, na.rm = TRUE)) {
        mean(x, na.rm = TRUE)
    } else {
        y <- x[which(!is.na(x))]
        unique <- unique(y)
        unique[which.max(tabulate(match(y, unique)))]
    }
}

This function average values for numeric variables and assigning the most frequent value (mode) for single integer or other type of variables. If no mode can be found, the function will return the first value of x.

Value

A tsibble object.

See Also

Other utility functions: find_epoch(), raw_data(), read_acttrust(), write_acttrust()

Examples

acttrust

aggregate_index(acttrust, unit = "hour")

aggregate_index(acttrust, unit = "day")

aggregate_index(acttrust, unit = "week")

aggregate_index(acttrust, unit = "month")

aggregate_index(acttrust, unit = "quarter")

aggregate_index(acttrust, unit = "year")

gipsousp/actverse documentation built on Oct. 6, 2023, 12:21 a.m.