diff --git a/NAMESPACE b/NAMESPACE index 448d8959..e2f0e28a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -14,8 +14,13 @@ S3method("[",yearquarter) S3method("[",yearweek) S3method("[[",interval) S3method("[[",tbl_ts) +S3method(anti_join,lst_ts) +S3method(anti_join,tbl_ts) S3method(append_row,data.frame) S3method(append_row,tbl_ts) +S3method(arrange,grouped_ts) +S3method(arrange,lst_ts) +S3method(arrange,tbl_ts) S3method(as.Date,yearmonth) S3method(as.Date,yearquarter) S3method(as.Date,yearweek) @@ -48,12 +53,17 @@ S3method(diff,yearquarter) S3method(diff,yearweek) S3method(fill_gaps,data.frame) S3method(fill_gaps,tbl_ts) +S3method(filter,lst_ts) S3method(format,interval) S3method(format,tbl_ts) S3method(format,yearmonth) S3method(format,yearquarter) S3method(format,yearweek) S3method(frequency,tbl_ts) +S3method(full_join,lst_ts) +S3method(full_join,tbl_ts) +S3method(group_by,lst_ts) +S3method(group_by,tbl_ts) S3method(guess_frequency,Date) S3method(guess_frequency,POSIXt) S3method(guess_frequency,numeric) @@ -77,6 +87,8 @@ S3method(index_valid,yearmonth) S3method(index_valid,yearqtr) S3method(index_valid,yearquarter) S3method(index_valid,yearweek) +S3method(inner_join,lst_ts) +S3method(inner_join,tbl_ts) S3method(interval_pull,Date) S3method(interval_pull,POSIXt) S3method(interval_pull,default) @@ -96,21 +108,38 @@ S3method(key_data,tbl_ts) S3method(key_drop_default,default) S3method(key_drop_default,tbl_ts) S3method(key_vars,tbl_ts) +S3method(left_join,lst_ts) +S3method(left_join,tbl_ts) S3method(measured_vars,tbl_ts) S3method(measures,tbl_ts) +S3method(mutate,lst_ts) +S3method(mutate,tbl_ts) S3method(new_data,tbl_ts) S3method(print,interval) S3method(print,yearmonth) S3method(print,yearquarter) S3method(print,yearweek) S3method(rbind,tbl_ts) +S3method(rename,grouped_ts) +S3method(rename,lst_ts) +S3method(rename,tbl_ts) S3method(rep,yearmonth) S3method(rep,yearquarter) S3method(rep,yearweek) +S3method(right_join,lst_ts) +S3method(right_join,tbl_ts) S3method(scan_gaps,tbl_ts) +S3method(select,grouped_ts) +S3method(select,lst_ts) +S3method(select,tbl_ts) +S3method(semi_join,lst_ts) +S3method(semi_join,tbl_ts) S3method(seq,yearmonth) S3method(seq,yearquarter) S3method(seq,yearweek) +S3method(slice,lst_ts) +S3method(slice,tbl_ts) +S3method(summarise,tbl_ts) S3method(tbl_sum,grouped_ts) S3method(tbl_sum,tbl_ts) S3method(time,Date) @@ -119,10 +148,15 @@ S3method(time,numeric) S3method(time,yearmonth) S3method(time,yearquarter) S3method(time,yearweek) +S3method(transmute,grouped_ts) +S3method(transmute,lst_ts) +S3method(transmute,tbl_ts) S3method(type_sum,tbl_ts) S3method(type_sum,yearmonth) S3method(type_sum,yearquarter) S3method(type_sum,yearweek) +S3method(ungroup,grouped_ts) +S3method(ungroup,tbl_ts) S3method(unique,yearmonth) S3method(unique,yearquarter) S3method(unique,yearweek) @@ -167,9 +201,12 @@ export(build_tsibble) export(count_gaps) export(difference) export(duplicates) +export(fill.grouped_ts) +export(fill.tbl_ts) export(fill_gaps) export(fill_na) export(filter_index) +export(gather.tbl_ts) export(group_by_key) export(guess_frequency) export(has_gaps) @@ -199,6 +236,7 @@ export(key_vars) export(measured_vars) export(measures) export(n_keys) +export(nest.tbl_ts) export(new_data) export(new_interval) export(new_tsibble) @@ -226,6 +264,7 @@ export(slide_dfc) export(slide_dfr) export(slide_tsibble) export(slider) +export(spread.tbl_ts) export(stretch) export(stretch2) export(stretch2_dfc) @@ -249,6 +288,8 @@ export(time_unit) export(tsibble) export(type_sum) export(units_since) +export(unnest.lst_ts) +export(unnest.tbl_ts) export(update_tsibble) export(yearmonth) export(yearquarter) @@ -270,16 +311,20 @@ importFrom(dplyr,"%>%") importFrom(dplyr,anti_join) importFrom(dplyr,arrange) importFrom(dplyr,filter) +importFrom(dplyr,full_join) importFrom(dplyr,group_by) importFrom(dplyr,group_data) importFrom(dplyr,group_rows) importFrom(dplyr,group_vars) importFrom(dplyr,grouped_df) importFrom(dplyr,groups) +importFrom(dplyr,inner_join) importFrom(dplyr,left_join) importFrom(dplyr,mutate) +importFrom(dplyr,rename) importFrom(dplyr,right_join) importFrom(dplyr,select) +importFrom(dplyr,semi_join) importFrom(dplyr,slice) importFrom(dplyr,summarise) importFrom(dplyr,transmute) diff --git a/R/dplyr-join.R b/R/dplyr-join.R index a7b7c26d..a802a41f 100644 --- a/R/dplyr-join.R +++ b/R/dplyr-join.R @@ -17,6 +17,7 @@ rename_join_tsibble <- function(x, y, by = NULL, suffix = c(".x", ".y")) { #' @inheritParams dplyr::left_join #' @rdname tsibble-tidyverse #' @include dplyr-verbs.R +#' @export left_join.tbl_ts <- function( x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ... ) { @@ -26,6 +27,7 @@ left_join.tbl_ts <- function( } #' @rdname tsibble-tidyverse +#' @export right_join.tbl_ts <- function( x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ... ) { @@ -35,6 +37,7 @@ right_join.tbl_ts <- function( } #' @rdname tsibble-tidyverse +#' @export inner_join.tbl_ts <- function( x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ... ) { @@ -44,6 +47,7 @@ inner_join.tbl_ts <- function( } #' @rdname tsibble-tidyverse +#' @export full_join.tbl_ts <- function( x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ... ) { @@ -53,12 +57,14 @@ full_join.tbl_ts <- function( } #' @rdname tsibble-tidyverse +#' @export semi_join.tbl_ts <- function(x, y, by = NULL, copy = FALSE, ...) { tbl <- NextMethod() update_meta(tbl, x, ordered = is_ordered(x), validate = FALSE) } #' @rdname tsibble-tidyverse +#' @export anti_join.tbl_ts <- function(x, y, by = NULL, copy = FALSE, ...) { tbl <- NextMethod() update_meta(tbl, x, ordered = is_ordered(x), validate = FALSE) diff --git a/R/dplyr-verbs.R b/R/dplyr-verbs.R index 94317d30..edde3624 100644 --- a/R/dplyr-verbs.R +++ b/R/dplyr-verbs.R @@ -22,6 +22,7 @@ #' #' @name tsibble-tidyverse #' @rdname tsibble-tidyverse +#' @export arrange.tbl_ts <- function(.data, ...) { exprs <- enquos(...) if (is_empty(exprs)) return(.data) @@ -31,6 +32,7 @@ arrange.tbl_ts <- function(.data, ...) { update_meta(arr_data, .data, ordered = ordered, interval = interval(.data)) } +#' @export arrange.grouped_ts <- function(.data, ..., .by_group = FALSE) { exprs <- enquos(...) if (is_empty(exprs)) return(.data) @@ -78,6 +80,7 @@ filter.tbl_ts <- function(.data, ..., .preserve = FALSE) { } #' @rdname tsibble-tidyverse +#' @export slice.tbl_ts <- function(.data, ..., .preserve = FALSE) { pos <- enquos(...) if (length(pos) > 1) { @@ -99,6 +102,7 @@ row_validate <- function(x) { } #' @rdname tsibble-tidyverse +#' @export select.tbl_ts <- function(.data, ...) { lst_quos <- enquos(...) lst_exprs <- map(lst_quos, quo_get_expr) @@ -118,16 +122,20 @@ select.tbl_ts <- function(.data, ...) { select_tsibble(.data, !!! lst_quos) } +#' @export select.grouped_ts <- select.tbl_ts #' @rdname tsibble-tidyverse +#' @export rename.tbl_ts <- function(.data, ...) { rename_tsibble(.data, ...) } +#' @export rename.grouped_ts <- rename.tbl_ts #' @rdname tsibble-tidyverse +#' @export mutate.tbl_ts <- function(.data, ...) { # mutate returns lst_ts without attributes, coerce to tbl_df first mut_data <- mutate(as_tibble(.data), ...) @@ -169,6 +177,7 @@ mutate.tbl_ts <- function(.data, ...) { } #' @rdname tsibble-tidyverse +#' @export transmute.tbl_ts <- function(.data, ...) { lst_quos <- enquos(..., .named = TRUE) mut_data <- mutate(.data, !!! lst_quos) @@ -177,6 +186,7 @@ transmute.tbl_ts <- function(.data, ...) { select_tsibble(mut_data, !!! vec_names, validate = FALSE) } +#' @export transmute.grouped_ts <- function(.data, ...) { res <- NextMethod() # keeping index and key @@ -195,8 +205,9 @@ transmute.grouped_ts <- function(.data, ...) { #' pedestrian %>% #' as_tibble() %>% #' summarise(Total = sum(Count)) +#' @export summarise.tbl_ts <- function(.data, ...) { - # Unlike summarise.grouped_df(), summarise.tbl_ts() doesn't compute values for + # Unlike summarise.grouped_df(), summarise.tbl_ts() doesn't compute values for # empty groups. Bc information is unavailable over the time range for empty # groups. idx <- index(.data) @@ -213,7 +224,7 @@ summarise.tbl_ts <- function(.data, ...) { grped_data <- group_by_index2(.data) grps <- groups(grped_data) len_grps <- length(grps) - sum_data <- + sum_data <- group_by( summarise(grped_data, !!! nonkey_quos), !!! grps[-((len_grps - 1):len_grps)] # remove index2 and last grp @@ -226,13 +237,14 @@ summarise.tbl_ts <- function(.data, ...) { grps <- setdiff(group_vars(.data), as_string(idx2)) build_tsibble( - sum_data, key = !! grps, index = !! idx2, regular = reg, ordered = TRUE, + sum_data, key = !! grps, index = !! idx2, regular = reg, ordered = TRUE, interval = NULL, validate = FALSE ) } #' @inheritParams dplyr::group_by #' @rdname tsibble-tidyverse +#' @export group_by.tbl_ts <- function(.data, ..., add = FALSE, .drop = group_by_drop_default(.data)) { lst_quos <- enquos(..., .named = TRUE) @@ -293,6 +305,7 @@ group_by_key <- function(.data, ..., .drop = key_drop_default(.data)) { } #' @rdname tsibble-tidyverse +#' @export ungroup.grouped_ts <- function(x, ...) { tbl <- ungroup(as_tibble(x)) build_tsibble_meta( @@ -301,6 +314,7 @@ ungroup.grouped_ts <- function(x, ...) { ) } +#' @export ungroup.tbl_ts <- function(x, ...) { attr(x, "index2") <- index(x) x diff --git a/R/tidyr-verbs.R b/R/tidyr-verbs.R index cd884864..373c00cc 100644 --- a/R/tidyr-verbs.R +++ b/R/tidyr-verbs.R @@ -12,6 +12,7 @@ #' ) #' (stocksm <- stocks %>% gather(stock, price, -time)) #' stocksm %>% spread(stock, price) +#' @export gather.tbl_ts <- function(data, key = "key", value = "value", ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE) { key <- as_string(enexpr(key)) @@ -38,6 +39,7 @@ gather.tbl_ts <- function(data, key = "key", value = "value", ..., #' @inheritParams tidyr::spread #' @rdname tsibble-tidyverse +#' @export spread.tbl_ts <- function(data, key, value, fill = NA, convert = FALSE, drop = TRUE, sep = NULL) { key <- enexpr(key) @@ -75,6 +77,7 @@ spread.tbl_ts <- function(data, key, value, fill = NA, convert = FALSE, #' stocksm %>% #' group_by(stock) %>% #' nest() +#' @export nest.tbl_ts <- function(data, ..., .key = "data") { nest_exprs <- enexprs(...) key_var <- expr_name(enexpr(.key)) @@ -119,6 +122,7 @@ nest.tbl_ts <- function(data, ..., .key = "data") { #' @examples #' nested_stock %>% #' unnest(key = stock) +#' @export unnest.lst_ts <- function(data, ..., key = NULL, .drop = NA, .id = NULL, .sep = NULL, .preserve = NULL ) { @@ -173,6 +177,7 @@ unnest.lst_ts <- function(data, ..., key = NULL, #' qtl = list(c("0%", "25%", "50%", "75%", "100%")) #' ) #' unnest(stock_qtl, key = qtl) +#' @export unnest.tbl_ts <- function(data, ..., key = NULL, .drop = NA, .id = NULL, .sep = NULL, .preserve = NULL ) { @@ -208,48 +213,64 @@ unnest_tsibble <- function(data, key, index) { #' @inheritParams tidyr::fill #' @rdname tsibble-tidyverse +#' @export fill.tbl_ts <- function(data, ..., .direction = c("down", "up")) { res <- NextMethod() update_meta2(res, data, ordered = is_ordered(data), interval = interval(data)) } #' @rdname tsibble-tidyverse +#' @export fill.grouped_ts <- fill.tbl_ts +#' @export mutate.lst_ts <- function(.data, ...) { as_lst_ts(NextMethod()) } +#' @export transmute.lst_ts <- mutate.lst_ts +#' @export select.lst_ts <- mutate.lst_ts +#' @export rename.lst_ts <- mutate.lst_ts +#' @export arrange.lst_ts <- mutate.lst_ts +#' @export filter.lst_ts <- mutate.lst_ts +#' @export slice.lst_ts <- mutate.lst_ts +#' @export group_by.lst_ts <- mutate.lst_ts +#' @export left_join.lst_ts <- function( x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ... ) { as_lst_ts(NextMethod()) } +#' @export right_join.lst_ts <- left_join.lst_ts +#' @export full_join.lst_ts <- left_join.lst_ts +#' @export inner_join.lst_ts <- left_join.lst_ts +#' @export anti_join.lst_ts <- function(x, y, by = NULL, copy = FALSE, ...) { as_lst_ts(NextMethod()) } +#' @export semi_join.lst_ts <- anti_join.lst_ts as_lst_ts <- function(x) { diff --git a/R/tsibble-pkg.R b/R/tsibble-pkg.R index edae0126..d639f436 100644 --- a/R/tsibble-pkg.R +++ b/R/tsibble-pkg.R @@ -71,10 +71,10 @@ #' @importFrom purrr map map_dbl map_int map_chr map_lgl #' @importFrom purrr map2 map2_dbl map2_int map2_chr map2_lgl #' @importFrom purrr pmap pmap_dbl pmap_int pmap_chr pmap_lgl -#' @importFrom dplyr arrange filter slice select mutate transmute summarise +#' @importFrom dplyr arrange filter slice select mutate transmute summarise rename #' @importFrom dplyr group_by ungroup group_data grouped_df group_vars #' @importFrom dplyr group_rows groups -#' @importFrom dplyr anti_join left_join right_join +#' @importFrom dplyr anti_join left_join right_join full_join semi_join inner_join #' @importFrom tibble new_tibble #' @import rlang tidyselect #' @examples diff --git a/R/zzz.R b/R/zzz.R index 98a45a7a..af9180e0 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -12,53 +12,9 @@ register_s3_method("pillar", "obj_sum", "yearmonth") register_s3_method("pillar", "obj_sum", "yearquarter") - register_s3_method("tidyr", "gather", "tbl_ts") - register_s3_method("tidyr", "spread", "tbl_ts") - register_s3_method("tidyr", "nest", "tbl_ts") - register_s3_method("tidyr", "unnest", "tbl_ts") - register_s3_method("tidyr", "unnest", "lst_ts") - register_s3_method("tidyr", "fill", "tbl_ts") - register_s3_method("tidyr", "fill", "grouped_ts") - - register_s3_method("dplyr", "mutate", "tbl_ts") - register_s3_method("dplyr", "transmute", "tbl_ts") - register_s3_method("dplyr", "transmute", "grouped_ts") - register_s3_method("dplyr", "select", "tbl_ts") - register_s3_method("dplyr", "select", "grouped_ts") - register_s3_method("dplyr", "rename", "tbl_ts") - register_s3_method("dplyr", "rename", "grouped_ts") - register_s3_method("dplyr", "arrange", "tbl_ts") - register_s3_method("dplyr", "arrange", "grouped_ts") register_s3_method("dplyr", "filter", "tbl_ts") - register_s3_method("dplyr", "slice", "tbl_ts") - register_s3_method("dplyr", "group_by", "tbl_ts") - register_s3_method("dplyr", "summarise", "tbl_ts") - register_s3_method("dplyr", "ungroup", "tbl_ts") - register_s3_method("dplyr", "ungroup", "grouped_ts") - register_s3_method("dplyr", "distinct", "tbl_ts") - - register_s3_method("dplyr", "left_join", "tbl_ts") - register_s3_method("dplyr", "right_join", "tbl_ts") - register_s3_method("dplyr", "full_join", "tbl_ts") - register_s3_method("dplyr", "inner_join", "tbl_ts") - register_s3_method("dplyr", "anti_join", "tbl_ts") - register_s3_method("dplyr", "semi_join", "tbl_ts") - - register_s3_method("dplyr", "mutate", "lst_ts") - register_s3_method("dplyr", "transmute", "lst_ts") - register_s3_method("dplyr", "select", "lst_ts") - register_s3_method("dplyr", "rename", "lst_ts") - register_s3_method("dplyr", "arrange", "lst_ts") register_s3_method("dplyr", "filter", "lst_ts") - register_s3_method("dplyr", "slice", "lst_ts") - register_s3_method("dplyr", "group_by", "lst_ts") - register_s3_method("dplyr", "left_join", "lst_ts") - register_s3_method("dplyr", "right_join", "lst_ts") - register_s3_method("dplyr", "full_join", "lst_ts") - register_s3_method("dplyr", "inner_join", "lst_ts") - register_s3_method("dplyr", "anti_join", "lst_ts") - register_s3_method("dplyr", "semi_join", "lst_ts") - + register_s3_method("dplyr", "distinct", "tbl_ts") register_s3_method("dplyr", "group_split", "tbl_ts") register_s3_method("dplyr", "group_split", "grouped_ts") register_s3_method("dplyr", "group_trim", "grouped_ts") diff --git a/man/tsibble-tidyverse.Rd b/man/tsibble-tidyverse.Rd index a1d29f12..92632eae 100644 --- a/man/tsibble-tidyverse.Rd +++ b/man/tsibble-tidyverse.Rd @@ -35,7 +35,7 @@ \method{select}{tbl_ts}(.data, ...) -rename.tbl_ts(.data, ...) +\method{rename}{tbl_ts}(.data, ...) \method{mutate}{tbl_ts}(.data, ...) @@ -54,13 +54,13 @@ rename.tbl_ts(.data, ...) \method{right_join}{tbl_ts}(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...) -inner_join.tbl_ts(x, y, by = NULL, copy = FALSE, suffix = c(".x", - ".y"), ...) +\method{inner_join}{tbl_ts}(x, y, by = NULL, copy = FALSE, + suffix = c(".x", ".y"), ...) -full_join.tbl_ts(x, y, by = NULL, copy = FALSE, suffix = c(".x", - ".y"), ...) +\method{full_join}{tbl_ts}(x, y, by = NULL, copy = FALSE, + suffix = c(".x", ".y"), ...) -semi_join.tbl_ts(x, y, by = NULL, copy = FALSE, ...) +\method{semi_join}{tbl_ts}(x, y, by = NULL, copy = FALSE, ...) \method{anti_join}{tbl_ts}(x, y, by = NULL, copy = FALSE, ...)