diff --git a/NEWS.md b/NEWS.md index 4aa92145..dab74b8d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,6 +12,7 @@ This release marks the migration of tsibble's S3 vector classes, such as `yearqu * Added `keyed_lag()`, `keyed_lead()`, and `keyed_difference()` that can be called in `mutate()` to respect tsibble's key and gaps. This improves efficiency in computing time and memory size. * The `.full` argument in `*_gaps` support two more options, `start()` and `end()`, for padding to the same starting or ending time. (#147) * `select()` a tsibble now keeps both index and key by default. (#155) +* Added `tidyr::drop_na()` support for tsibble. (#173) ## Bug fixes diff --git a/R/error.R b/R/error.R index 2258f1ea..26cd0575 100644 --- a/R/error.R +++ b/R/error.R @@ -35,9 +35,9 @@ not_tsibble <- function(x) { pkg_not_available <- function(pkg, min_version = NULL) { pkg_lgl <- requireNamespace(pkg, quietly = TRUE) if (!pkg_lgl) { - if (is_null(min_version)) { - abort(sprintf("Package `%s` required.\nPlease install and try again.", pkg)) - } + abort(sprintf("Package `%s` required.\nPlease install and try again.", pkg)) + } else if (pkg_lgl && is_null(min_version)) { + return() } else if (utils::packageVersion(pkg) < min_version) { abort(sprintf( "Package `%s` (>= v%s) required.\nPlease install and try again.", diff --git a/R/tidyr-verbs.R b/R/tidyr-verbs.R index f8adebc9..230dcd37 100644 --- a/R/tidyr-verbs.R +++ b/R/tidyr-verbs.R @@ -11,7 +11,6 @@ #' ) #' (stocksm <- stocks %>% gather(stock, price, -time)) #' stocksm %>% spread(stock, price) - gather.tbl_ts <- function(data, key = "key", value = "value", ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE) { key <- as_string(enexpr(key)) @@ -166,3 +165,10 @@ fill.tbl_ts <- function(data, ..., .direction = c("down", "up")) { } fill.grouped_ts <- fill.tbl_ts + +drop_na.tbl_ts <- function(data, ...) { + res <- NextMethod() + update_meta2(res, data, ordered = is_ordered(data), interval = interval(data)) +} + +drop_na.grouped_ts <- drop_na.tbl_ts diff --git a/R/zzz.R b/R/zzz.R index d0439ae6..c70d45a7 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -10,6 +10,8 @@ s3_register("tidyr::spread", "tbl_ts") s3_register("tidyr::fill", "grouped_ts") s3_register("tidyr::fill", "tbl_ts") + s3_register("tidyr::drop_na", "tbl_ts") + s3_register("tidyr::drop_na", "grouped_ts") s3_register("tidyr::nest", "tbl_ts") s3_register("tidyr::nest", "grouped_ts") s3_register("tidyr::unnest", "tbl_ts") diff --git a/tests/testthat/test-tidyr.R b/tests/testthat/test-tidyr.R index 9bd0d608..d2eba77b 100644 --- a/tests/testthat/test-tidyr.R +++ b/tests/testthat/test-tidyr.R @@ -121,4 +121,27 @@ test_that("fill()", { as_tibble() %>% fill(kilo, .direction = "down") ) + expect_is(fill(harvest_fill, kilo), "tbl_ts") + expect_is(fill(group_by_key(harvest_fill), kilo), "grouped_ts") +}) + +test_that("drop_na() #173", { + expect_equal( + harvest_fill %>% + group_by_key() %>% + drop_na(), + harvest_fill %>% + as_tibble() %>% + group_by(fruit) %>% + drop_na() + ) + expect_equal( + harvest_fill %>% + drop_na(), + harvest_fill %>% + as_tibble() %>% + drop_na() + ) + expect_is(drop_na(harvest_fill), "tbl_ts") + expect_is(drop_na(group_by_key(harvest_fill)), "grouped_ts") })