View source: R/collapse_index.R
collapse_by | R Documentation |
Collapse the index of a tbl_time
object by time period. The index column
is altered so that all dates that fall in a specified interval share a
common date.
collapse_by( .tbl_time, period = "year", start_date = NULL, side = "end", clean = FALSE, ... )
.tbl_time |
A |
period |
A character specification used for time-based grouping. The
general format to use is Note that you can pass the specification in a flexible way:
This shorthand is available for year, quarter, month, day, hour, minute, second, millisecond and microsecond periodicities. Additionally, you have the option of passing in a vector of dates to use as custom and more flexible boundaries. |
start_date |
Optional argument used to specify the start date for the first group. The default is to start at the closest period boundary below the minimum date in the supplied index. |
side |
Whether to return the date at the beginning or the end of the new period. By default, the "end" of the period. Use "start" to change to the start of the period. |
clean |
Whether or not to round the collapsed index up / down to the next period boundary. The decision to round up / down is controlled by the side argument. |
... |
Not currently used. |
collapse_by()
is a simplification of a call to dplyr::mutate()
to collapse an
index column using collapse_index()
.
# Basic functionality ------------------------------------------------------- # Facebook stock prices data(FB) FB <- as_tbl_time(FB, date) # Collapse to weekly dates collapse_by(FB, "weekly") # A common workflow is to group on the collapsed date column # to perform a time based summary FB %>% collapse_by("year") %>% dplyr::group_by(date) %>% dplyr::summarise_if(is.numeric, mean) # Grouped functionality ----------------------------------------------------- data(FANG) FANG <- FANG %>% as_tbl_time(date) %>% dplyr::group_by(symbol) # Collapse each group to monthly, # calculate monthly standard deviation for each column FANG %>% collapse_by("month") %>% dplyr::group_by(symbol, date) %>% dplyr::summarise_all(sd)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.