Learn R Programming

tsibble (version 0.8.2)

tsibble-tidyverse: Tidyverse methods for tsibble

Description

  • arrange(): if not arranging key and index in past-to-future order, a warning is likely to be issued.

  • slice(): if row numbers are not in ascending order, a warning is likely to be issued.

  • select(): keeps the variables you mention as well as the index.

  • transmute(): keeps the variable you operate on, as well as the index and key.

  • summarise() reduces a sequence of values over time instead of a single summary, as well as dropping empty keys/groups.

Usage

# S3 method for tbl_ts
arrange(.data, ...)

# S3 method for tbl_ts filter(.data, ..., .preserve = FALSE)

# S3 method for tbl_ts slice(.data, ..., .preserve = FALSE)

# S3 method for tbl_ts select(.data, ...)

# S3 method for tbl_ts rename(.data, ...)

# S3 method for tbl_ts mutate(.data, ...)

# S3 method for tbl_ts transmute(.data, ...)

# S3 method for tbl_ts summarise(.data, ...)

# S3 method for tbl_ts gather(data, key = "key", value = "value", ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE)

# S3 method for tbl_ts spread(data, key, value, ...)

# S3 method for tbl_ts nest(.data, ...)

Arguments

.data

A tbl_ts.

...

Same arguments accepted as its tidyverse generic.

.preserve

when FALSE (the default), the grouping structure is recalculated based on the resulting data, otherwise it is kept as is.

data

A data frame.

key

Names of new key and value columns, as strings or symbols.

This argument is passed by expression and supports quasiquotation (you can unquote strings and symbols). The name is captured from the expression with rlang::ensym() (note that this kind of interface where symbols do not represent actual objects is now discouraged in the tidyverse; we support it here for backward compatibility).

value

Names of new key and value columns, as strings or symbols.

This argument is passed by expression and supports quasiquotation (you can unquote strings and symbols). The name is captured from the expression with rlang::ensym() (note that this kind of interface where symbols do not represent actual objects is now discouraged in the tidyverse; we support it here for backward compatibility).

na.rm

If TRUE, will remove rows from output where the value column is NA.

convert

If TRUE will automatically run type.convert() on the key column. This is useful if the column types are actually numeric, integer, or logical.

factor_key

If FALSE, the default, the key values will be stored as a character vector. If TRUE, will be stored as a factor, which preserves the original ordering of the columns.

Details

Column-wise verbs, including select(), transmute(), summarise(), mutate() & transmute(), keep the time context hanging around. That is, the index variable cannot be dropped for a tsibble. If any key variable is changed, it will validate whether it's a tsibble internally. Use as_tibble() to leave off the time context.

Examples

Run this code
# NOT RUN {
library(dplyr, warn.conflicts = FALSE)
# Sum over sensors
pedestrian %>%
  index_by() %>% 
  summarise(Total = sum(Count))
# shortcut
pedestrian %>%
  summarise(Total = sum(Count))
# Back to tibble
pedestrian %>%
  as_tibble() %>%
  summarise(Total = sum(Count))
library(tidyr)
# example from tidyr
stocks <- tsibble(
  time = as.Date('2009-01-01') + 0:9,
  X = rnorm(10, 0, 1),
  Y = rnorm(10, 0, 2),
  Z = rnorm(10, 0, 4)
)
(stocksm <- stocks %>% gather(stock, price, -time))
stocksm %>% spread(stock, price)
nested_stock <- stocksm %>% 
  nest(-stock)
stocksm %>% 
  group_by(stock) %>% 
  nest()
# }

Run the code above in your browser using DataLab