R: Summarise each group by time

summarise_by_time {tidyquant} | R Documentation

## Summarise each group by time

### Description

`summarise_by_time()` Is a time-series variant of the popular `dplyr::summarise()` function.

`summarise_by_time()` and `summarize_by_time()` are synonyms.

### Usage

    summarise_by_time(
      .data,
      .date_var,
      ...,
      .by = "week",
      .type = c("floor", "ceiling", "round")
    )

    summarize_by_time(
      .data,
      .date_var,
      ...,
      .by = "week",
      .type = c("floor", "ceiling", "round")
    )

### Arguments

`.data` | A `tbl` object or `data.frame`
`.date_var` | A column of date or date-time (e.g. POSIXct) data class
`...` | Name-value pairs of summary functions. The name will be the name of the variable in the result. 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 data frame, to add multiple columns from a single expression.
`.by` | A time unit to summarise by. Time units are collapsed using `lubridate::floor_date()` or `lubridate::ceiling_date()`. The value can be: `second` `minute` `hour` `day` `week` `month` `bimonth` `quarter` `season` `halfyear` `year` Arbitrary unique English abbreviations as in the `lubridate::period()` constructor are allowed.
`.type` | One of "floor", "ceiling", or "round. Defaults to "floor". See `lubridate::round_date`.

### Value

An object usually of the same type as `.data`.

- The rows come from the underlying `group_keys()`.

- The columns are a combination of the grouping keys and the summary expressions that you provide.

- If `x` is grouped by more than one variable, the output will be another grouped_df with the right-most group removed.

- If `x` is grouped by one variable, or is not grouped, the output will be a tibble.

- Data frame attributes are not preserved, because `summarise()` fundamentally creates a new data frame.

### Useful summary functions

- Sum: `SUM()`

- Center: `AVERAGE()`, `MEDIAN()`

- Spread: `STDEV()`, `VAR()`

- Range: `MIN()`, `MAX()`

- Count: `COUNT()`, `COUNT_UNIQUE()`

- Position: `FIRST()`, `LAST()`, `NTH()`

- Correlation: `COR()`, `COV()`

### Methods

This function is a generic, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.

### Examples

    # Libraries
    library(tidyquant)
    library(dplyr)

    # First adjusted price in each month
    FANG %>%
        group_by(symbol) %>%
        summarise_by_time(
            .date_var  = date,
            .by         = "month",
            adjusted   = FIRST(adjusted)
        )

    # Last adjused price in each month (day is last day of month with ceiling option)
    FANG %>%
        group_by(symbol) %>%
        summarise_by_time(
            .date_var  = date,
            .by        = "month",
            adjusted   = LAST(adjusted),
            .type      = "ceiling")

    # Total Volume each year (.by is set to "year" now)
    FANG %>%
        group_by(symbol) %>%
        summarise_by_time(
            .date_var  = date,
            .by        = "year",
            adjusted   = SUM(volume))

[Package tidyquant version 1.0.0 Index]