Skip to contents

Creates a rolling count UpdateByOp for each column in cols, using ticks as the windowing unit.

Arguments

cols

String or list of strings denoting the column(s) to operate on. Can be renaming expressions, i.e. “new_col = col”. Default is to compute the rolling count for all non-grouping columns.

rev_ticks

Integer scalar denoting the look-behind window size in number of rows.

fwd_ticks

Integer scalar denoting the look-ahead window size in number of rows. Default is 0.

Value

UpdateByOp to be used in a call to update_by().

Details

Ticks are row counts, and you may specify the reverse and forward window in number of rows to include. The current row is considered to belong to the reverse window but not the forward window. Also, negative values are allowed and can be used to generate completely forward or completely reverse windows. Here are some examples of window values:

  • rev_ticks = 1, fwd_ticks = 0 - contains only the current row

  • rev_ticks = 10, fwd_ticks = 0 - contains 9 previous rows and the current row

  • rev_ticks = 0, fwd_ticks = 10 - contains the following 10 rows, excludes the current row

  • rev_ticks = 10, fwd_ticks = 10 - contains the previous 9 rows, the current row and the 10 rows following

  • rev_ticks = 10, fwd_ticks = -5 - contains 5 rows, beginning at 9 rows before, ending at 5 rows before the current row (inclusive)

  • rev_ticks = 11, fwd_ticks = -1 - contains 10 rows, beginning at 10 rows before, ending at 1 row before the current row (inclusive)

  • rev_ticks = -5, fwd_ticks = 10 - contains 5 rows, beginning 5 rows following, ending at 10 rows following the current row (inclusive)

This function acts on aggregation groups specified with the by parameter of the update_by() caller function. The aggregation groups are defined by the unique combinations of values in the by columns. For example, if by = c("A", "B"), then the aggregation groups are defined by the unique combinations of values in the A and B columns.

This function, like other Deephaven uby functions, is a generator function. That is, its output is another function called an UpdateByOp intended to be used in a call to update_by(). This detail is typically hidden from the user. However, it is important to understand this detail for debugging purposes, as the output of a uby function can otherwise seem unexpected.

For more information, see the vignette on uby functions by running vignette("update_by").

Examples

if (FALSE) { # \dontrun{
library(rdeephaven)

# connecting to Deephaven server
client <- Client$new("localhost:10000", auth_type = "psk", auth_token = "my_secret_token")

# create data frame, push to server, retrieve TableHandle
df <- data.frame(
  timeCol = seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date() + 0.01), by = "1 sec")[1:500],
  boolCol = sample(c(TRUE, FALSE), 500, TRUE),
  col1 = sample(10000, size = 500, replace = TRUE),
  col2 = sample(10000, size = 500, replace = TRUE),
  col3 = 1:500
)
th <- client$import_table(df)

# compute rolling count of col1 and col2, using the previous 5 rows and current row
th1 <- th$
  update_by(uby_rolling_count_tick(cols = c("col1RollCount = col1", "col2RollCount = col2"), rev_ticks = 6))

# compute rolling count of col1 and col2, grouped by boolCol, using previous 5 rows, current row, and following 5 rows
th2 <- th$
  update_by(uby_rolling_count_tick(cols = c("col1RollCount = col1", "col2RollCount = col2"), rev_ticks = 6, fwd_ticks = 5), by = "boolCol")

# compute rolling count of col1 and col2, grouped by boolCol and parity of col3, using current row and following 10 rows
th3 <- th$
  update("col3Parity = col3 % 2")$
  update_by(uby_rolling_count_tick(cols = c("col1RollCount = col1", "col2RollCount = col2"), rev_ticks = 1, fwd_ticks = 10), by = c("boolCol", "col3Parity"))

client$close()
} # }