Skip to content

Commit

Permalink
support tidyr::drop_na()
Browse files Browse the repository at this point in the history
resolve #173
  • Loading branch information
earowang committed Mar 9, 2020
1 parent 8de986b commit e65d42c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(<tbl_ts>)` 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

Expand Down
6 changes: 3 additions & 3 deletions R/error.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 7 additions & 1 deletion R/tidyr-verbs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-tidyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})

0 comments on commit e65d42c

Please sign in to comment.